note 104160 added to function.filter-var
| From: | php at maisqi dot com | Date: | Fri, 27 May 2011 14:11:15 +0000 |
| Subject: | note 104160 added to function.filter-var | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-180073@lists.php.net to get a copy of this message | ||
FILTER_VALIDATE_URL does not support internationalized domain name (IDN). Valid or not, no domain
name with Unicode chars on it will pass validation.
We can circumvent this with a home grown solutions, but C code is C code, so I've gone for the
code bellow, which builds on filter_var().
<?php
$res = filter_var ($uri, FILTER_VALIDATE_URL);
if ($res) return $res;
// Check if it has unicode chars.
$l = mb_strlen ($uri);
if ($l !== strlen ($uri)) {
// Replace wide chars by âXâ.
$s = str_repeat (' ', $l);
for ($i = 0; $i < $l; ++$i) {
$ch = mb_substr ($uri, $i, 1);
$s [$i] = strlen ($ch) > 1 ? 'X' : $ch;
}
// Re-check now.
$res = filter_var ($s, FILTER_VALIDATE_URL);
if ($res) { $uri = $res; return 1; }
}
?>
The logic is simple. A non-ascii char is more than one byte long. We replace every one of those
chars by "X" and check again.
An alternative will be to punycode the URI before calling filter_var(), but PHP lacks native support
for punycode. I think my approach is effective. Please e-mail me if you think otherwise or see room
for improvement.
----
Server IP: 81.92.192.129
Probable Submitter: 95.92.92.137
----
Manual Page -- http://www.php.net/manual/en/function.filter-var.php
Edit -- https://master.php.net/note/edit/104160
Del: integrated -- https://master.php.net/note/delete/104160/integrated
Del: useless -- https://master.php.net/note/delete/104160/useless
Del: bad code -- https://master.php.net/note/delete/104160/bad+code
Del: spam -- https://master.php.net/note/delete/104160/spam
Del: non-english -- https://master.php.net/note/delete/104160/non-english
Del: in docs -- https://master.php.net/note/delete/104160/in+docs
Del: other reasons-- https://master.php.net/note/delete/104160
Reject -- https://master.php.net/note/reject/104160
Search -- https://master.php.net/manage/user-notes.php