Returning part of string between others with preg_replace

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
zeeight
Forum Newbie
Posts: 3
Joined: Fri Mar 23, 2007 9:55 am

Returning part of string between others with preg_replace

Post by zeeight »

Ok, this is sort of a regex and function thing. Can you tell that I am not a programmer?

A few notes about the project:

This is for an online version of a company's handbook / operating guidelines. This is being built on Wordpress (so you may see some functions specific to that).

Code: Select all

<?php
  /* Custom function for hiding parts of the post */
  function hide_this($text, $userlevel = '2' ) 
      {
           global $userdata;
           get_currentuserinfo();
           if ($userdata->user_level < $userlevel)
           {
                $text = '';
           } 
           else 
           {
                echo $text;
           }
      } 
 /* End function */
                            
           $currentPage = get_page($page_id);
           $fullContent = $currentPage->post_content;
           preg_replace(
			 "#(::[ ]*hide[ ]*::(.+)::[ ]*/hide[ ]*::)#imseU",
			 " /* THIS IS WHERE I AM CONFUSED */ ",
			 $fullContent
		         );               
                        
?>

So, the idea here is that anywhere that I have the following setup:

Code: Select all

::hide::
This should be hidden from normal viewers.
::/hide::
This should be visible to normal viewers.
I do a preg_replace to replace everything between-and-including the ::hide:: "tags" with the output of the hide_this(); function.

My checks on the user and whatnot (the Wordpress-centric parts of this) work fine, but I'm not sure what I should be replacing everything with (see the THIS IS WHERE I AM CONFUSED section of the code). Honestly, I'm not sure that this is the best way to go about it.

I think that I need something like:

Code: Select all

preg_replace(
	    "#(::[ ]*hide[ ]*::(.+)::[ ]*/hide[ ]*::)#imseU",
	    "hide_this(THE STUFF BETWEEN THE TWO TAGS)",
	    $fullContent
            );  
But I'm not sure how to refer to that -- or if it's even possible. Thoughts? Am I going about this in completely the wrong way?
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

could use some tweaking I'm sure, but it sounds like you want:

Code: Select all

$filteredContent = preg_replace('#:: ?hide ?::[\w\d\s\r\n\Z.]*?:: ?/hide ?::#i','',$fullContent);
You might find this useful:
http://www.cuneytyilmaz.com/prog/jrx/
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Code: Select all

#::\s*hide\s*::(?s:\s*(.*?)\s*)::\s*/hide\s*::#mi
may be better.

(untested)
zeeight
Forum Newbie
Posts: 3
Joined: Fri Mar 23, 2007 9:55 am

Post by zeeight »

Thanks for the help tweaking my replacing between the two ::hide:: tags.

I should have been more clear, however -- the point of this is to hide the information between the tags from some users, but not from others. Basically, I was wondering if there was a way to:

(1) Remove the tags
(2) Run the $text that was between the two tags through my function to determine if it should be displayed to the user or not.

Thanks,
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

preg_replace_callback(). Avoid the "e" modifier.
zeeight
Forum Newbie
Posts: 3
Joined: Fri Mar 23, 2007 9:55 am

Post by zeeight »

<span style='color:blue' title='I&#39;m naughty, are you naughty?'>smurf</span> tight -- thanks a bunch
User avatar
stereofrog
Forum Contributor
Posts: 386
Joined: Mon Dec 04, 2006 6:10 am

Post by stereofrog »

Checking user level for every "hide" tag found looks like a waste of time to me. Why not to go the other way around: check permissions once and then remove "hidden" portions only if necessary:

Code: Select all

get_currentuserinfo();
if ($userdata->user_level < $userlevel) 
	$fullContent = preg_replace(
		'~::hide::.*?::/hide::~si', 
		'', 
		$fullContent);
Post Reply