ZF2有urlencode的别名吗?

3

zf2中是否有urlencode的别名?

在zf2中,如果我想要进行rawurlencode,我可以使用:

$escaper = new Zend\Escaper\Escaper();
echo $escaper->escapeUrl('hello world');

这将输出:

hello%20world

然而,我应该如何称呼urlencode?期望的输出是:
hello+world

如果简短的回答是我只需要直接调用urlencode,那就这样吧。

这是一个本地函数,你为什么需要一些包装器呢? - Cheery
@Cheery,zf2在这里解释了上下文转义:http://framework.zend.com/manual/2.1/en/modules/zend.escaper.theory-of-operation.html - Leo Galleguillos
1个回答

1
短答案是否定的。原生的rawurlencode()函数根据RFC 3986生成输出,但urlencode()则不是。这是在escapeUrl()方法中使用rawurlencode的主要动机。我认为在这种情况下您有两个选择; A. 您可以尝试扩展原生的Escaper并覆盖escapeUrl()方法:
namespace My\Escaper;

use Zend\Escaper\Escaper as BaseEscaper;

class Escaper extends BaseEscaper
{
    /**
     * {@inheritDoc}
     */
    public function escapeUrl($string)
    {
        return urlencode($string);
    }
}

B. 您可以像评论中@cheery所说的那样简单地使用urlencode(),这是一种本地函数。(个人认为这是最简单的解决方案)

更新:

您还可以阅读此答案,了解urlencode和rawurlencode之间的区别。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接