我有一个问题,通过链接发送信息(字符串)后,我想用 + 号替换字符串中的空格,这就是我所做的
html:
<a href="script.php?name=Eri+Son">send</a>
脚本:
$name = $_GET['name'];
str_replace(" ", "+", $name);
echo $name;
result: Eri Son
您没有将返回的值分配给str_replace
变量$name
。尝试:
$name = str_replace(" ", "+", $name);
如果要将文字+
作为查询字符串参数发送,则必须对其进行 urlencode:
<a href="script.php?name=<?php echo urlencode('Eri+Son'); ?>">send</a>
结果:
<a href="script.php?name=Eri%2BSon">send</a>
在服务器端,$_GET[name]
将包含Eri+Son
. 其他答案以及您对查询字符串参数执行 str_replace 的想法是错误的。
尝试这个
echo preg_replace(' ', '+', $name);