当通过 URL 传递字符串时,空格将被忽略


space is neglected when passed a string by URL

我使用以下代码在表中显示超链接:

echo "<td><a href=http://www.smstoneta.com/show.php?opcode=TCP Y".">".
        $row['num_y']."</a></td>";
超链接

显示成功,但是当我单击超链接时,URL是

www.smstoneta.com/show.php?opcode=TCP

而不是

www.smstoneta.com/show.php?opcode=TCP Y

为什么我没有得到完整的网址?

use urlencode()

$opCode = urlencode('TCP Y');
echo "<td><a href=http://www.smstoneta.com/show.php?opcode=".$opCode.">".$row['num_y']."</a></td>";

您需要 URL 编码空间才能使它们在链接中工作。

这是PHP函数urlencode的手册

$safe_url = urlencode('http://www.smstoneta.com/show.php?opcode=TCP Y');
echo "<td><a href=" .$safe_url. ">" .$row['num_y']. "</a></td>";

顺便说一句,回显此类字符串的更具可读性(无需连接)的版本是:

echo "<td><a href='{$safe_url}'>{$row['num_y']}</a></td>";