Re: Weird replacing

From: Date: Fri, 26 Sep 2003 18:35:01 +0000
Subject: Re: Weird replacing
Groups: php.general 
Request: Send a blank email to php-general+get-164055@lists.php.net to get a copy of this message
I figured it out already. if the array has http://example.com/bbs/index.php?board=1 http://example.com/bbs/index.php?board=1;action=display;thread=12 3 then, the str_replace of that one with the other one that has the symbols changed, will be http://example.com/bbs/index.php/board-1 but it'll ALSO change the other one! http://example.com/bbs/index.php/board-1;action=display;thread=12 3 so when str_replace is looking for a match for the second url, it doesn't find it, cause it's not the same anymore I added this, right before the for loop, so larger strings will be replaced first and shorter last. rsort($match_spider, SORT_STRING); Thanks for "listening" to me while I was trying to figure it out anyway :) Cristian Anguz Web Design wrote: > Hi, > > I wrote a code to change the urls in my forum, to a new format. I > did it with output buffering and inside it, I used > preg_replace_callback, which worked great for me. But I shared > this code with the forum program community and there's a person > that can't make it work. He says his Apache gets caught up in a > loop or something. So I looked for an alternative to > preg_replace_callback. The way I did it is with preg_match_all > and then changed each element in the array and saved the changed > elements in a new array, then I just str_replace using the arrays > as find/replace. This solutions worked fine, almost. I use two > functions to do changes to the urls, one to make them relative > and the other to change symbols in the query, to something more > friendly to search-engine spiders. The function to make them > relative works great with the new method, as it did with > preg_replace_callback, but the other function, the one to replace > symbols in the query, is acting in an odd way. > > Here's function > > <?php > function spider_url($match){ > if(!strstr($match['2'], 'action') || strstr($match['2'], > 'display') || strstr($match['2'], 'messageindex')) > return $match['1'] . '/' . str_replace(array("=", > ";", > "&"), array("-", "_", "_"), $match['2']); > return $match['0']; > } >> > > if I call it with this > > <?php > /// $scripturl is the var that has the url to the forum > script, like http://example.com/bbs/index.php > $buffer = preg_replace_callback('/(' . preg_quote($scripturl, > '/') . ')\?([\w;=~&+%]+)/i', 'spider_url', $buffer); >> > > it works great and the url > > http://example.com/bbs/index.php?board=1;action=display;thread=12 > 3;start=1 > > changes to > > http://example.com/bbs/index.php/board-1_action-display_thread-12 > 3_start-1 > > but if I do it with this > > <?php > preg_match_all('/(' . preg_quote($scripturl, '/') . > ')\?([\w;=~&+%]+)/i', $buffer, $match_spider, PREG_SET_ORDER); > for($i = 0; $i < count($match_spider); $i++){ > $arr1_spider[$i] = $match_spider[$i]['0']; > $arr2_spider[$i] = spider_url($match_spider[$i]); > } > $buffer = str_replace($arr1_spider, $arr2_spider, $buffer); >> > > then the url comes out like this > > http://example.com/bbs/index.php/board-1;action=display;thread=12 > 3;start=1 > > It changes just the first part, up to the semicolon... I did > other tests and there was one where changed everything up to the > semicolon right before 'star'.... > > I can't understand what's going on, so if someone has a clue, > please tell me. Thank you very much in advance for taking your > time reading this. > > Cristian

« previous php.general (#164055) next »