Welcome Guest, Not a member yet? Register   Sign In
ci way to validate url
#1

[eluser]keevitaja[/eluser]
what is the best practice to validate an url?
now the local characters like üõäö etc ara also permitted!

any thoughts?
#2

[eluser]keevitaja[/eluser]
this expression doesn't validata IDN urls. so i came up with a solution.

i found idn converter from
https://github.com/aluksidadi/IDN-Valida....class.php

and then extended url helper
preg_match validates only the existense of top level domain, cause filter_var passes urls without it. like http://domain

Code:
function validate_url($url) {
  if(!preg_match("/^http(|s):\/{2}(.*)\.([a-z]){2,}(|\/)(.*)$/i", $url)) {
    return false;
  }
  
  $ci = & get_instance();
  $ci->load->library('idna_convert');
  
  $url = $ci->idna_convert->encode($url);
  
  return filter_var($url, FILTER_VALIDATE_URL);
}

if anyone has better solution or approach, i would be glad to hear it!
#3

[eluser]Amitabh Roy[/eluser]
Code:
filter_var($url, FILTER_VALIDATE_URL);

rejects domain name with dashes even if the domain is valid.
There is a bug in php 5.2 which prevents it from validating url with dashes.
#4

[eluser]keevitaja[/eluser]
how about 5.3

bug still present?

edit: anyway 5.3.1 validates this one:

http://test-öä.ee/this-is-test
#5

[eluser]Amitabh Roy[/eluser]
[quote author="keevitaja" date="1326973473"]how about 5.3

bug still present?

edit: anyway 5.3.1 validates this one:

http://test-öä.ee/this-is-test[/quote]


Great, it works then. Need to add it to my note.




Theme © iAndrew 2016 - Forum software by © MyBB