Does zf2 have an alias for urlencode?
Does zf2 have an alias for urlencode
?
In zf2, if I want to do rawurlencode
, I use:
$escaper = new Zend\Escaper\Escaper();
echo $escaper->escapeUrl('hello world');
What are the outputs:
hello%20world
However, what would I call urlencode
? Desired output:
hello+world
If the short answer is that I just need to call directly urlencode
, then so be it.
source to share
The short answer is no. The native function rawurlencode()
produces output in accordance with RFC 3986 , however it urlencode()
does not. This is the main motivation for using rawurlencode in a method escapeUrl()
. I think you have two options in this case:
A .. You can try extending the native Escaper and overriding 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 just use urlencode()
as pointed out in the comments in @cheery's comment, this is a native function. (Personally, I think this is the simplest solution)
UPDATE:
You can also read this answer about the difference between urlencode and rawurlencode in depth.
source to share