1

我有一个 Zend Framework URI,例如 /controller/action/var1/value1/var2/value2 。

  1. Value2 中有一个空格字符。我应该如何在 PHP 中对其进行编码?当我使用带有 value2 的 urlencode 时,这会将空格转换为“+”而不是“%20f”。那样可以么?

  2. 这个 value2 也被客户端的 javascript 添加到一个 href 位置。我在那里使用了转义功能,但是当我单击链接时,我在 Firefox 地址栏中既看不到“+”也看不到“%20f”。虽然当我在 firebug 'net' 选项卡中看到它时,我看到了 %20f。

我应该在 PHP 和 javascript 中使用哪些函数?

4

1 回答 1

2

urlencode关于您的第一个问题,这就是和之间的区别rawurlencode

var_dump(urlencode("hello, world"));

会得到你:

string 'hello%2C+world' (length=14)

尽管

var_dump(rawurlencode("hello, world"));

会得到你:

string 'hello%2C%20world' (length=16)

我想两者都应该没问题;但请随意尝试一下,以防万一;-)


关于第二点:Firefox 试图让 URL 变得“更漂亮”,以人类可读的方式显示它们,而不是编码 - 这对我们开发人员不利,但对最终用户有利。

例如,如果我在 Firefox 的地址栏中键入此 URL:

http://tests/temp/temp.php?a=hello%2C%20world

当我按下enter键时,它会自动翻译成

http://tests/temp/temp.php?a=hello%2C%20world

如果它按照您的方式工作(并且,当您看到使用 Firebug 的编码 URL 时,它可能正在工作),一切正常 ;-)

于 2009-12-24T15:14:24.727 回答