How to use a global regular expression

How to work with a multiline regular expression

      $string = "Ray Harris\nAuthor"; // A multiline string
      $pattern1 = '/Harris$/';  // A non-multiline regular expression
      preg_match($pattern1, $string); // Does not match Harris and returns 0
      $pattern2 = '/Harris$/m';  // A multiline regular expression
      preg_match($pattern2, $string); // Matches Harris and returns 1
      

How to work with a global regular expression

      $string = 'MBT-6745 MBT-5712'; 
      $pattern = '/MBT-[[:digit:]]{4}/'; 
      $count = preg_match_all($pattern, $string, $matches); 
      foreach ($matches[0] as $match) {
        echo("<div>$match&ly;/div>\n");
      }
      

Description

Back