Google Ads

PHP RegEx: Match a pattern at the end of the string PHP Script


PHP RegEx: Match a pattern at the end of the string PHP Script

In previous tutorial we have a pattern that match a string at the beggining using caret(^).
Now we try to match the pattern at the end of the string using \z.

What is \z? This is end of subject or read at the end of subject.
We still use (i) case insensitive modifier so that it can read any case.

 

 
 
  1. <?php
  2. // create a string
  3. $string = 'Ronard';
  4. //display the string Ronard
  5. echo "<b>String is: </b>".$string."<br/>";
  6. // try to match our pattern at the end of the string
  7. if(preg_match("/nard\z/i", $string)) {
  8. // if our pattern matches we echo this
  9. echo 'The string ends with nard';
  10. } else {
  11. // if no match is found we echo this line
  12. echo 'No match found';
  13. }
  14. ?>

 

 

The above script will generate “The string ends with nard” which is read at the end of the subject.

 

 

Have any Question or Comment?

Leave a Reply

Your email address will not be published. Required fields are marked *

"