phpcharacter-encodingurlencodefilter-var

php - filter_var returns false even with urlencode


$url="http://www.example.com/日本語/こんにちは.html";
$result=filter_var(urlencode($url), FILTER_VALIDATE_URL);

I am encoding the URL! Then, why is $return false?


Solution

  • You use urlencode to encode query parameter values. It encodes characters that have a special meaning in a url, like :, /, ?, etc.

    If you encode an entire url - as opposed to values in the query section - it is not a valid url any more.

    This is your url after you encode it:

    http%3A%2F%2Fwww.example.com%2F%E6%97%A5%E6%9C%AC%E8%AA%9E%2F%E3%81%93%E3%82%93%E3%81%AB%E3%81%A1%E3%81%AF.html