Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Nov 2011
    Posts
    2
    Thanks
    2
    Thanked 0 Times in 0 Posts

    PHP regular expression

    Hi, I have a question about regular expression.

    $ means matching from the end of a string and ^ from the beginning. However, why does the following expression return failure?

    $mode = "/^php$/";
    $string = "phpphp";
    preg_match($mode, $string);

    what I think is that both the beginning and end contain 'php', so it should returns 1, but it doesn't.

    Thanks a lot,

  • #2
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,638
    Thanks
    2
    Thanked 404 Times in 396 Posts
    You're more or less saying "does 'phpphp' == 'php'?"

    Here is the literal translation of your regular expression:

    Code:
    ^php$
    
    Assert position at the beginning of the string «^»
    Match the characters “php” literally «php»
    Assert position at the end of the string (or before the line break at the end of the string, if any) «$»
    It doesn't match because you're trying to match literal string "php" with "phpphp".


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •