preg_match not working

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dinesh1985singh
    New Member
    • Apr 2010
    • 49

    #1

    preg_match not working

    Hello everyone,

    I am trying to use preg_match for replacing a string.
    I am using following code

    Code:
    $regExp = "/^[i]$/";
    $match = "hii this";
    $result = preg_replace($regExp,"s",$match);
    echo $result ; // results into hii this
    The preg_replace is not replacing the matching character.
    But if i use $regExp = "/[i]/"; instead of $regExp = "/^[i]$/";
    it works ,Can I get the reason behind this .I guess the
    $regExp = "/^[i]$/"; should also be work.The caret(^) and the dollar($) just specify the start and end of the regular expression.


    Thanks
    Last edited by Atli; Mar 27 '11, 03:51 PM. Reason: Please use [code] tags when posting code.
  • dgreenhouse
    Recognized Expert Contributor
    • May 2008
    • 250

    #2
    This '$regExp = "/^[i]$/";' is telling the regex to match the character class [i] at the beginning and end of the subject string only. And this '$regExp = "/[i]/";' is telling it to match anywhere in the subject string.

    Comment

    • dinesh1985singh
      New Member
      • Apr 2010
      • 49

      #3
      Thank you dgreenhouse for your quick reply. I get the thing now.
      Thank you

      Comment

      Working...