Google Ads

PHP RegEx: Match the Beginning of the String PHP Script


PHP RegEx: Match the Beginning of the String PHP Script

In previous tutorial we match the given string. Now we are going to match at the beginning of the given string.

 

 
 
  1. <?php
  2. // make a string
  3. $string = 'Ronard';
  4. echo "<b>String: </b>".$string."<br>";
  5. // match the beginning of the string
  6. if(preg_match("/^Ron/", $string))
  7. {
  8. // if it matches we display this line
  9. echo 'The string begins with Ron';
  10. }
  11. else
  12. {
  13. // if no match is found display this line
  14. echo 'No match found';
  15. }
  16. ?>

 

The regex character to read beginning is the caret (^). Then we see if the string given begin with Ron.
The forward slashes (/) we see are the delimeter that hold our regex pattern. And the quotations are used to ‘wrap it all’.
Then we see the (^) will put as the beggining of the string.

 

 

Have any Question or Comment?

Leave a Reply

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

"