【问题标题】:PHP - str_replace not working with URL variable stringPHP - str_replace 不适用于 URL 变量字符串
【发布时间】:2014-04-02 01:40:07
【问题描述】:

我正在将 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 方法不会删除 $url 变量中放置在 href 标记中的字符串部分。 '&store' 的值出现在源代码和浏览器的实际链接中。

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

非常感谢任何建议!!!

【问题讨论】:

  • 为什么不在“某些条件”下添加变量而不是添加然后再次删除它?
  • 您的伪if 语句中有错字(缺少结束")。

标签: php url str-replace


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-01-06
    • 2013-10-23
    • 2011-07-10
    • 1970-01-01
    • 1970-01-01
    • 2013-12-17
    • 1970-01-01
    相关资源
    最近更新 更多