The Little Guy Posted April 24, 2007 Share Posted April 24, 2007 Anyone Know why str_replace isn't working? What it is doing, is making the $newURL contain search=word, it never switches. Example: URL: /search/search?page=2&search=word&q=game When the link "Web" is generated, it should change the above URL to this: /search/search?page=2&search=web&q=game The problem is, that the link "Web" is always the first URL, and doesn't change to the second one in the source code. Make Sense? <?php $endURL = strtolower($_SERVER['REQUEST_URI']); $wrdMatch = array("search=word","search=web"); $wrdReplace = array("search=web","search=word"); if(!isset($_GET['search'])||$_GET['search']=='web'){ echo ' <strong>Web</strong> '; }else{ $newURL = str_replace($wrdMatch,$wrdReplace,$endURL); echo ' <a href="'.$newURL.'">Web</a> '; } if($_GET['search']=='word'){ echo ' <strong>Word Match</strong> '; }else{ $newURL = str_replace($wrdMatch,$wrdReplace,$endURL); echo ' <a href="'.$newURL.'">Word Match</a> '; } ?> Link to comment https://forums.phpfreaks.com/topic/48407-str_replace/ Share on other sites More sharing options...
benjaminbeazy Posted April 24, 2007 Share Posted April 24, 2007 i'm not sure exactly what's happening but I couldn't solve it using str_replace the way you have it. i think that the php.net manual has an example that mimics the issue that you're having (check at the bottom of the examples, http://us2.php.net/manual/en/function.str-replace.php) and the only way around it i can see, would be to put the vars between each conditional, like: $wrdMatch = "search=word"; $wrdReplace = "search=web"; $wrdReplace = "search=web"; $wrdMatch = "search=word"; Link to comment https://forums.phpfreaks.com/topic/48407-str_replace/#findComment-236713 Share on other sites More sharing options...
rajatn Posted April 24, 2007 Share Posted April 24, 2007 In your Code , str_replace -- Replace all occurrences of the search, so "search=word" is replaced by "search=web" and again "search=web" is replaced by "search=word" thats why the change is not reflected.......... Link to comment https://forums.phpfreaks.com/topic/48407-str_replace/#findComment-236855 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.