preg_match_all php code example

Example 1: preg_match in php

<?php
//Syntex : int preg_match( $pattern, $input, $matches, $flags, $offset)
  
// Declare a variable and initialize it 
$str = "Check For Testing."; 
  
// case-Insensitive search for the word "Check" 
if (preg_match("/\bCheck\b/i", $str, $match))  
    echo "Matched!"; 
else
    echo "not matched";   
  

// Output : Matched
?>

Example 2: php preg match space or start of string

/\b(stackoverflow)\b/

Example 3: preg_match in php

<?php

$my_text="I Love Regular Expressions";

$my_array  = preg_split("/ /", $my_text);

print_r($my_array );

?>

Example 4: preg_match in php

<?php
function_name('/pattern/',subject);
?>

Example 5: 12 hrs for preg match with single and double digit in php

((1[0-2]|0?[1-9]):([0-5][0-9]) ?([AaPp][Mm]))

12:00 pm    ---------     OK
13:00
1:00 am    ---------     OK
5:5 am
5:05 PM    ---------     OK 
55:55 
09:59    // valid time, but meridiem is missing
:01
0:59
00:59 PM

Example 6: preg_match in php

<html>
   
   <head>
      <title>Hello World</title>
   </head>
   
   <body>
      <?php echo "Hello, World!";?>
   </body>

</html>

Tags:

Php Example