我有一个网页,其中包含一个超链接,如下所示:
$name = "Hello World";
echo "<a href='page.php?name='". preg_replace(" ", "_", $name) "'> bla bla </a>"
这将成功生成以下链接:
...page.php?name=Hello_World
在我的page.php中,我尝试反转操作:
if($_SERVER[REQUEST_METHOD] == "GET"){
$name = $_GET['name'];
//testing if the problem is with GET
echo $name;
//now here's the problem:
$string = preg_replace("_", " ", $name);
echo $string;
}
$ name正确地回显,但是$ string始终为null我已经尝试了所有可能的组合,例如〜〜和/ /和[_]和\ s,并直接使用$ _GET像这样:
preg_replace("_", " ", $_GET['name']);
他们都没有工作。这个问题困扰了我大部分时间。任何帮助表示赞赏。
preg_replace
接受正则表达式作为第一个参数。无论是" "
也不"_"
是有效的正则表达式。
在这种情况下,您可以使用str_replace
。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句