通过URL传递字符串时忽略空格

时间:2013-10-14 20:37:49

标签: php html

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

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

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

2 个答案:

答案 0 :(得分:4)

使用urlencode()

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

答案 1 :(得分:1)

您需要URL Encode个空格才能让它们在链接中工作。

这是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>";

BTW,更可读(不需要连接)版本来回显这样的字符串是:

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