tags:

views:

89

answers:

2

hi, i want to use php to detect the string which is called $myoutput & check it if it's a valid link syntax or just a normal text.

the function should recognise all links formats including the ones contains GET parameters

it is preferred to not call that link to see if it's a valid link syntax by doing CURL or file_get_contents.

maybe with some preg match. or another solution

Thanks

+5  A: 

Use the native Filter Validator

filter_var($url, FILTER_VALIDATE_URL);

Example:

if(filter_var($url, FILTER_VALIDATE_URL) === FALSE)
    die('Not a valid URL');
}

See this tutorial about filter_var usage

Gordon
FILTER_VALIDATE_URL does not work correctly and should be avoided for now. As it always return false on URLs that contain '-'
Nazariy
+1  A: 

You can use preg_match.

$pattern = '/^(?:[;\/?:@&=+$,]|(?:[^\W_]|[-_.!~*\()\[\] ])|(?:%[\da-fA-F]{2}))*$/';
$string = 'some-url';
if( preg_match( $pattern, $string ) == 1 ) {
   // url is valid
}
Jacob Relkin