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 Link to comment https://forums.phpfreaks.com/topic/70355-using-array-in-preg_replace_callback/ 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].\"]"' Link to comment https://forums.phpfreaks.com/topic/70355-using-array-in-preg_replace_callback/#findComment-353414 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. Link to comment https://forums.phpfreaks.com/topic/70355-using-array-in-preg_replace_callback/#findComment-353419 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. Link to comment https://forums.phpfreaks.com/topic/70355-using-array-in-preg_replace_callback/#findComment-353421 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. Link to comment https://forums.phpfreaks.com/topic/70355-using-array-in-preg_replace_callback/#findComment-353422 Share on other sites More sharing options...
Jessica Posted September 23, 2007 Share Posted September 23, 2007 You could write one Link to comment https://forums.phpfreaks.com/topic/70355-using-array-in-preg_replace_callback/#findComment-353425 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); Link to comment https://forums.phpfreaks.com/topic/70355-using-array-in-preg_replace_callback/#findComment-353427 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.. Link to comment https://forums.phpfreaks.com/topic/70355-using-array-in-preg_replace_callback/#findComment-353429 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? Link to comment https://forums.phpfreaks.com/topic/70355-using-array-in-preg_replace_callback/#findComment-354017 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.