jasonbullard Posted September 23, 2007 Share Posted September 23, 2007 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 Quote Link to comment Share on other sites More sharing options...
BlueSkyIS Posted September 23, 2007 Share Posted September 23, 2007 try removing ampersand? '&$matches' becomes '$matches' also: there is no return in your function. you may need one. e.g., 'return "[start=\".$matches[0].\"]"' Quote Link to comment Share on other sites More sharing options...
jasonbullard Posted September 23, 2007 Author Share Posted September 23, 2007 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. Quote Link to comment Share on other sites More sharing options...
BlueSkyIS Posted September 23, 2007 Share Posted September 23, 2007 fyi, i'm new to preg_replace_callback, but i'll try to help. I assume $this->reserved is a string?? is an array a valid second parameter for preg_replace_callback? my understanding is that it should be a function name. Quote Link to comment Share on other sites More sharing options...
jasonbullard Posted September 23, 2007 Author Share Posted September 23, 2007 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. Quote Link to comment Share on other sites More sharing options...
Jessica Posted September 23, 2007 Share Posted September 23, 2007 You could write one Quote Link to comment Share on other sites More sharing options...
BlueSkyIS Posted September 23, 2007 Share Posted September 23, 2007 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); Quote Link to comment Share on other sites More sharing options...
jasonbullard Posted September 23, 2007 Author Share Posted September 23, 2007 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.. Quote Link to comment Share on other sites More sharing options...
effigy Posted September 24, 2007 Share Posted September 24, 2007 How about arraying the create_functions first, then passing that array to preg_replace_callback? Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.