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
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.

Link to comment
Share on other sites

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.  :)

Link to comment
Share on other sites

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..  :(

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.