0

我有一个问题,通过链接发送信息(字符串)后,我想用 + 号替换字符串中的空格,这就是我所做的

html:

<a href="script.php?name=Eri+Son">send</a>

脚本:

 $name = $_GET['name'];
    str_replace(" ", "+", $name);
    echo $name;

result: Eri Son
4

3 回答 3

2

您没有将返回的值分配给str_replace变量$name。尝试:

$name = str_replace(" ", "+", $name);

于 2013-01-26T13:00:51.190 回答
1

如果要将文字+作为查询字符串参数发送,则必须对其进行 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 的想法是错误的。

于 2013-01-26T14:03:41.110 回答
0

尝试这个

echo preg_replace(' ', '+', $name);
于 2013-01-26T13:01:31.827 回答