Pages

Thursday, September 13, 2012

Regular expression for (Filtering the URLS from a given string)

<?php

// The Regular Expression filter for urls (http://www.sometext.com)
$reg_exUrl = "/(http|https|ftp|ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/";
$text = "The text you want to filter goes here. http://www.google.com";
if(preg_match($reg_exUrl, $text, $url)) {
       echo $url[0];
} else {
    echo "No patterns matched"
}

// The Regular Expression filter for urls (www.sometext.com)
$reg_exUrl = "/(www|WWW|)\.[a-zA-Z0-9\-\.]+\.(com|in|)?/";
$text = 'some texts  http://www.yahoo.in/bin/set/?ilc=37 some texts';
if(preg_match($reg_exUrl, $text, $url)) {
       echo $url[0];
} else {
    echo "No patterns matched"
}
?>

No comments:

Post a Comment