• We just launched and are currently in beta. Join us as we build and grow the community.

PHP RegEx: With Meta Characters

mikigood

DeFi Protocol Auditor
M Rep
0
0
0
Rep
0
M Vouches
0
0
0
Vouches
0
Posts
47
Likes
53
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 200 XP
In our last tutorials regarding ReGex we have some simple pattern of matching. We also use caret (^) For matching at the beginning of the string and (\z) to match at the end of the string. These characters are called Meta Characters and shown below.

a. . (Full stop)
b. ^ (Carat)
c. * (Asterix)
d. + (Plus)
e. ? (Question Mark)
f. { (Opening curly brace)
g. [ (Opening brace)
h. ] (Closing brace)
i. \ (Backslash)
g. | (Pipe)
k. ( (Opening parens)
l. ) (Closing parens)
m. } (Closing curly brace)

Above characters are very useful in terms of ReGex. But if you search a character that include with special character of Meta Characters. Then we need to escape it using backslash.

Sample string: 1+2=3
  1. <?php

  2. // create a string
  3. $string

    =

    '1+2=3'

    ;
  4. // try to match our pattern

  5. if

    (

    preg_match

    (

    "/^1\+2/i"

    ,

    $string

    )

    )

    {

  6. // if the pattern matches we echo this
  7. echo

    'The string begins with 1+2'

    ;
  8. }

    else

    {

  9. // if no match is found we echo this line

  10. echo

    'No match found'

    ;
  11. }
  12. ?>

From the code above you will see the script that display:
"The string begins with 1+2" because if found the pattern 1+2 and escape the special meaning of + symbol in Meta characters.
I you would not escape the the character preg_match

(

"/^1+1/i/"

,

$string

)

;

then it will display "No match found".

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.

FOR YOUR OWN SAFETY, PLEASE:

1. Re-scan downloaded files using your personal virus checker before using it.

2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.


Download
You must upgrade your account or reply in the thread to view the hidden content.
 

452,496

336,313

336,321

Top