Search code examples
phpurlreplace

PHP - str_replace not working with URL variable string


I am building URL variables into another variable named '$url' below:

$url = "page2.php?";
$url .= "&Keyword=$keyword";

$shopByStore = $_GET["store"];
if (!empty($shopByStore)) {
    $url .= "&store=$shopByStore";
}
// plus many more variables built into the URL variable based on user input.

Under certain conditions, I will need to remove portions of the $url variable string. The str_replace method is not removing the portion of the string in the $url variable placed in the href tags. The value for '&store' is appearing in the source code and in the actual link in the browser.

if ($foo == "certain_condition) {
    str_replace("&store=$shopByStore", "", $url);
    ?>
    <a href="<?php echo $url; ?>">Clear</a><br>
    <?php
}

Any advice is greatly appreciated!!!


Solution

  • str_replace returns the modified string, it does not alter the string that you pass to it.

    Try: $url = str_replace("&store=$shopByStore", "", $url);