Regex in PHP

preg_split

$str = "Jane\tKate\nLucy Marion"; $regex = "@\s@"; // Output: Array("Jane", "Kate", "Lucy", "Marion") print\_r(preg\_split($regex, $str)); 

preg_grep

$arr = ["Jane", "jane", "Joan", "JANE"]; $regex = "/Jane/"; // Output: Jane echo preg\_grep($regex, $arr); 

preg_matchall

$regex = "/[a-zA-Z]+ (\d+)/"; $input\_str = "June 24, August 13, and December 30"; if (preg\_match\_all($regex, $input\_str, $matches\_out)) { // Output: 2 echo count($matches\_out); // Output: 3 echo count($matches\_out[0]); // Output: Array("June 24", "August 13", "December 30") print\_r($matches\_out[0]); // Output: Array("24", "13", "30") print\_r($matches\_out[1]); } 

preg_match

$str = "Visit QuickRef"; $regex = "#quickref#i"; // Output: 1 echo preg\_match($regex, $str); 

preg_replace

$str = "Visit Microsoft!"; $regex = "/microsoft/i"; // Output: Visit QuickRef! echo preg\_replace($regex, "QuickRef", $str); 
Comments