//Some useful regular expression pattern for our daily need in web applications//
$string="hikiran hellokiran iam kiran hellos hellosdsdsd ima ";
$pattern = "/(hi|hello)(kiran)/"; ///
preg_match_all($pattern,$string,$match); //aray zero will give exact match results array one will gives matches for first () parenthsessi array 2 give matches for parenthisis ()//
////////////////////////////////////////////////////////////////////////////////////
$string="hikiran hellokiran iam kiran hellos hellosdsdsd ima ";
$pattern = "/(.kiran)/"; ///
preg_match_all($pattern,$string,$match);
$string="<a href='kiran.php'>HOME LINK</a> ";
$pattern = "/[a-zA-Z0-9]i/"; /// any letter+i [charcacter class] - inside braces or is applied
preg_match_all($pattern,$string,$match);
////////////////////////////////////////////////////////////////////////////////////////
//Stripping email addresses from a string //
$string="iam@gmail.com kiran_123@yahoo.com sfsdf sdfsdfs sdfsdfsdf test@yahoo.co iniyahoo.com tester@yahoo hello @yahoo.com ";
//$pattern = "/[a-zA-Z0-9_.]+[@]{1}[a-zA-Z0-9_]+[.][a-zA-Z]{2,4}/"; /// get all emailss//
//$pattern = "/([a-zA-Z0-9_.]+)[@]{1}([a-zA-Z0-9_]+)[.]([a-zA-Z]{2,4})/"; /// +i
//this will give matches and the parts of an email //
preg_match_all($pattern,$string,$match);
$string="http://www.google.com https://www.testworld.com http:google.com";
$pattern = "/(http|https):\/\/[w]{3}.{1}[a-zA-Z0-9_]+.[a-zA-Z0-9]{2,5}/"; /// get all website address //
preg_match_all($pattern,$string,$match);
#####LINK EXTRACTION USING PHP#########
$string="<a href='index.php' class='test_php'>Home Page</a> <a href='contact.php'>Contact Us</a> ";
$pattern = "/a\s+[a-zA-Z]*href\s*=[\"']+([a-zA-Z._\s]+)[\"']+[\sa-zA-Z0-9'\"=_.]*>{1}([a-zA-Z0-9_.\s=]+)[<]{1}[.\s]*/"; /// get all emailss//
preg_match_all($pattern,$string,$match);
print_r($match[1]);
print_r($match[2]);
|