3

Does zf2 have an alias for urlencode?

In zf2, if I want to do a rawurlencode, I use:

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

Which outputs:

hello%20world

However, how would I call urlencode? The desired output is:

hello+world

If the short answer is that I simply need to call urlencode directly, then so be it.

Leo Galleguillos
  • 2,429
  • 3
  • 25
  • 43

1 Answers1

1

Short answer is no. The native rawurlencode() function produces output according to RFC 3986 however urlencode() is not. This is main motivation behind the usage of rawurlencode in escapeUrl() method. I think you have two options in this case;

A. You can try to extend native Escaper and override the escapeUrl() method:

namespace My\Escaper;

use Zend\Escaper\Escaper as BaseEscaper;

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

B. You can simply use urlencode() like @cheery's said in the comments, it's a native function. (Personally i think this is most simple solution)

UPDATE:

You may also want to read this answer about difference between urlencode and rawurlencode in deep.

Community
  • 1
  • 1
edigu
  • 9,878
  • 5
  • 57
  • 80