Skip to content

Commit

Permalink
Update filter_var_url function.
Browse files Browse the repository at this point in the history
Limonade no longer requires the filter extension (http://www.php.net/manual/en/book.filter.php),  so it will improve compatibility with older PHP versions.
Note that the regexp is quite permissive and might be improved
  • Loading branch information
Fabrice Luraine committed Sep 10, 2010
1 parent 71cc00a commit 76d16d7
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 16 deletions.
30 changes: 14 additions & 16 deletions lib/limonade.php
Expand Up @@ -2504,23 +2504,21 @@ function array_replace( array &$array, array &$array1 )
}
}

if(PHP_VERSION == '5.2.13' || PHP_VERSION == '5.3.2')
{
# There is a bug with filter_var($site_url , FILTER_VALIDATE_URL);
# http://www.mail-archive.com/php-bugs@lists.php.net/msg134778.html
function filter_var_url($str)
{
$regexp = '@^http(s)?://[-[:alnum:]]+\.[-[:alnum:]]+\.[a-zA-Z]{2,4}(:[0-9]+)?(.*)?$@';
$options = array( "options" => array("regexp" => $regexp ));
return filter_var($str, FILTER_VALIDATE_REGEXP, $options);
}
}
else
/**
* Check if a string is an url
*
* This implementation no longer requires
* {@link http://www.php.net/manual/en/book.filter.php the filter extenstion},
* so it will improve compatibility with older PHP versions.
*
* @param string $str
* @return false, str the string if true, false instead
*/
function filter_var_url($str)
{
function filter_var_url($str)
{
return filter_var($str, FILTER_VALIDATE_URL);
}
$regexp = '@^https?://([-[:alnum:]]+\.)+[a-zA-Z]{2,6}(:[0-9]+)?(.*)?$@';
$options = array( "options" => array("regexp" => $regexp ));
return preg_match($regexp, $str) ? $str : false;
}


Expand Down
6 changes: 6 additions & 0 deletions tests/main.php
Expand Up @@ -186,8 +186,14 @@ function test_main_url_for()
assert_equal(url_for('one', '', 'three'), '/one/three');
assert_equal(url_for('one', null, 'three'), '/one/three');
assert_equal(url_for('my/hash#test'), '/my/hash#test');

$site_url = 'http://www.limonade-php.net';
assert_true((bool) filter_var_url($site_url));
assert_true((bool) filter_var_url('http://example.com'));
assert_true((bool) filter_var_url('http://example.com:2000/'));
assert_true((bool) filter_var_url('https://www.example.com:2000'));
assert_true((bool) filter_var_url('http://test.example.com/?var1=true&var2=34'));
assert_false(filter_var_url('not an url'));

assert_equal(url_for($site_url), $site_url);
//var_dump(url_for($site_url, 'examples'));
Expand Down

0 comments on commit 76d16d7

Please sign in to comment.