Page 1 of 1

Returning part of string between others with preg_replace

Posted: Fri Mar 23, 2007 10:23 am
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?

Posted: Fri Mar 23, 2007 10:40 am
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/

Posted: Fri Mar 23, 2007 1:08 pm
by feyd

Code: Select all

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

(untested)

Posted: Fri Mar 23, 2007 1:16 pm
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,

Posted: Fri Mar 23, 2007 1:26 pm
by feyd
preg_replace_callback(). Avoid the "e" modifier.

Posted: Fri Mar 23, 2007 1:30 pm
by zeeight
<span style='color:blue' title='I&#39;m naughty, are you naughty?'>smurf</span> tight -- thanks a bunch

Posted: Sat Mar 24, 2007 5:24 am
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);