JavaScript regex and function to check valid URL

<script>
function ValidUrl(str) {
var url_protocol = /^(http|https|ftp):\/\//i;
var url_domain = /\.[a-z][a-z]/i;
var url_ip_v4address = /(\d{1,3}\.){3}\d{1,3}/;
if ( str.trim().length > 8 && url_protocol.test(str) && ( url_domain.test(str) || url_ip_v4address.test(str) ) )
return true; else return false;
}
</script>

PHP function decode URL as valid URL

<?php
function URLDecodeAsURL( $URL ) {
$URL = urldecode( $URL );
     $endcode = array (
          [0] => '&#32;',
          [1] => '&#39;',
          [2] => '&#34;',
          [3] => '&#38;',
          [4] => '&#63;',
          [5] => '&#35;',
          [6] => '&#60;',
          [7] => '&#62;',
          [8] => '&#61;'
     );

     $decode = array (
          [0] => ' ',
          [1] => '\'',
          [2] => '"',
          [3] => '&',
          [4] => '?',
          [5] => '#',
          [6] => '<',
          [7] => '>',
          [8] => '='
     );
return preg_replace($endcode, $decode, $URL);
}