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.

 

 

    <?php
    // make a string
    $string = 'Ronard';
     
    echo "<b>String: </b>".$string."<br>";
    // match the beginning of the string
    if(preg_match("/^Ron/", $string))
    {
    // if it matches we display this line
    echo 'The string begins with Ron';
    }
    else
    {
    // if no match is found display this line
    echo 'No match found';
    }
    ?>

 

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.

 

 

Read Me

 

Have any Question or Comment?

Leave a Reply

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