Hire PHP Web Developer
regular expression in php examples
TO CHECK ALPHA-NUMERIC STRING
$subject = "hi to all 123";
if(preg_match('/[^A-Za-z0-9\ ]/i',$subject))
{
echo "error";
}
else
{
echo "success";
}
FIND SPECIFIC LINK FROM WEBPAGE
$content = @file_get_contents("http://www.somewebpage.com/somepage.html");
$trans = array('/'=>'\/');
$link = strtr("http://www.searchdomain.com",$trans);
$regexp = '/<a\s[^>]*href=\"'.$link.'\"[^>]*>searchdomain<\/a>/siU';
if(@preg_match($regexp, $content))
{
echo "Match";
}
else
{
echo "Not Match";
}
FIND EMAIL FROM FOLLOWING CONTENT
$content = "Aamir [aamir@yahoo.com], Alice [alice@yahoo.com]";
$friends_array = explode(',',$content);
for($i=0; $i<count($friends_array); $i++)
{
preg_match('/[^>]*\[(.*)\]/Ui',$friends_array[$i],$matches);
print_r($matches);
}
GET ALL LINKS FROM WEBPAGE
$page = @file_get_contents("http://www.somewebpage.com/somepage.html");
$reg_expr = '/<a\s[^>]*href=(\"??)([^\" >]*?)\\1[^>]*>(.*)<\/a>/siU';
preg_match_all($reg_expr,$page,$matches);