Jump to content

str_replace


The Little Guy

Recommended Posts

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

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

Archived

This topic is now archived and is closed to further replies.

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