Jump to content

Using Array in preg_replace_callback


jasonbullard

Recommended Posts

Hopefully someone can help me cause I am at wits end.  It is probably and easy solution but I am just tired and can't think.  What I am trying to achieve here is to have a function that calls another one through preg_replace_callback.

 

In the called function I need to take an array and search through a string.  If a match is found then I need to encapsulate the found item in some preset tags.  The array could contain hundreds of words but I need it to be as fast as possible and I figured this was the best method if it can be done.

 

$myarray = array("one", "two", "three", "four");

preg_replace_callback($myarray, create_function('&$matches', '[start=".$matches[0]."]'), $str);

 

Now I have tried to remove the create_function and put it in its own but that does not work either.  Any help is appreciated.

 

Thanks,

Jason

Link to comment
https://forums.phpfreaks.com/topic/70355-using-array-in-preg_replace_callback/
Share on other sites

I have tried that and here is a trimmed down version of my code.

 

function start($str = '')
{
	$str = preg_replace_callback("the_regex_here", array($this, 'replace_whole'), $str);

	return $str;
}

function replace_whole(&$match)
{
	$return = '';

	$return = preg_replace_callback($this->reserved, array($this, 'replace_word'), $match[2]);

	return $return;
}

function replace_word($match)
{
	print_r($match);
}

 

Couple of things to note is that I call function start and that callback works.  In replace_whole that is the one that does not work.  Also, $match[2] is the correct parameter to be searched.  Everything works but the function replace_word does not seem to be executing at all.

Thats cool.  I am just at wits end.  I am thinking this isn't possible but I will keep trying.

 

$this->reserved is actually an array of strings

 

$this->reserved = array("one", "two", "three", "four");

 

Because it is contained in a class the second parameter must be an array of $this, 'function_name'.  It would make it so much easie if they had a str_replace_callback.  :)

Research has verified the need to provide an array for a class function call. However, an example on this page uses 'self' instead of $this:

 

http://us.php.net/preg_replace_callback,

 

$output = preg_replace_callback($pattern, array('self', 'do_something_callback'), $input);

You could write one ;)

 

Got any suggestions on where to start?  I am sure it contains a for or foreach loop maybe or some form of callback?

 

@BlueSkyIS,

 

Yeah, there a couple of ways to actually call it but I don't think there is an actual documented difference.  But, I will try it anyway just cause I am done..  :(

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.