CodeIgniter Forums
ci way to validate url - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: ci way to validate url (/showthread.php?tid=48465)



ci way to validate url - El Forum - 01-17-2012

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

any thoughts?


ci way to validate url - El Forum - 01-19-2012

[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-Validator/blob/master/idna_convert.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!


ci way to validate url - El Forum - 01-19-2012

[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.


ci way to validate url - El Forum - 01-19-2012

[eluser]keevitaja[/eluser]
how about 5.3

bug still present?

edit: anyway 5.3.1 validates this one:

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


ci way to validate url - El Forum - 01-19-2012

[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.