PHP - str_replace 不适用于 URL 变量字符串

问题描述 投票:0回答:1

我正在将 URL 变量构建到另一个名为“$url”的变量中:

$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.

在某些情况下,我需要删除 $url 变量字符串的部分内容。 str_replace 方法不会删除放在 href 标记中的 $url 变量中的字符串部分。 “&store”的值出现在源代码和浏览器中的实际链接中。

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

非常感谢任何建议!!!

php url replace
1个回答
3
投票

str_replace
返回修改后的字符串,它不会更改您传递给它的字符串。

尝试:

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

© www.soinside.com 2019 - 2024. All rights reserved.