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 Coder
    Join Date
    Feb 2006
    Posts
    39
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Backreferences in text being used in regular expressions.

    Sometimes I have $1 in text, which are being parsed by regular expressions. The regular expressions are taking the $1 in the text literally. e.g.

    $text = "$1 cookie.";
    $replace = "cookie from $2.";
    $text = preg_replace("/cookie/i", $replace, $text);

    print $text;
    Text will output "$1 cookie from ..". How can I avoid this?

  • #2
    New Coder
    Join Date
    Feb 2006
    Posts
    39
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Figured it out:

    Code:
    while (preg_match($pattern, $text,  $match))
      $text = str_replace($match[0], $replacement, $text);


  •  

    Posting Permissions

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