Scriptplayground

 Print Page | Close Window

Simple Way to Validate Links

Validate url's before you accept them in your scripts.

<?php
function validLink($link) {
    if(preg_match("/http:\/\//", $link)) {
        return true;
    } else {
        return false;
    }
}
?>

This simple little function checks a link for the correct http://. This could be taken a step further and checked against a series of proper protocals (http, ftp, feed) etc...

Most of the magic is done in the preg_match function. We check for an http://, but must escape certain characters within this function so we use \ to escape the forward slashes /.

That all thats too this function, if you have some ideas or possible expansions. Let me know, maybe they could be added to this tutorial.


Find this article at:
http://scriptplayground.com/tutorials/php/Simple-Way-to-Validate-Links/