SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    Community Advisor silver trophybronze trophy
    dresden_phoenix's Avatar
    Join Date
    Jun 2008
    Location
    Madison, WI
    Posts
    2,819
    Mentioned
    34 Post(s)
    Tagged
    2 Thread(s)

    MAMP/LAMP backreference syntax?

    This should be fairly simple, but it keeps failing (w/o reporting any errors) so I assume I am missing something from the syntax OR I have configred MAMP wrong.

    Basically I am trying to use a back reference within my preg_match. for example, to match tag, any tag, and its contents.


    PHP Code:
    $a="<em>some text goes here.</em>dshashd xxc dsfa";
    $r=preg_match("/<([a-z]+)>[^<]*<\/\1>/"$a$match); 
    echo (
    $r)? $match[1]: "nope"
    The first (and only) capturing group gathers the 'tag' and the \1 backreferences the capture pattern. have This works (as far as the simple test of finding a tag) on just about any PURE regexp tester, but on my local installation of MAMP (PHP 5.26) it always returns FALSE. Is there a difference syntax for backreferences in MAMP or did I go astray here in some other fashion?

  2. #2
    SitePoint Addict
    Join Date
    Apr 2011
    Posts
    265
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Hi,
    Try the dfollowing variant I tested it and it works:
    [code]$a="<em>some text goes here.</em>dshashd xxc dsfa";
    preg_match('#\<([a-z]+)\>[^\<]*\<\/\1\>#i', $a, $match);
    echo $match[1] ? $match[1]: "nope";[code]
    The '<', and '>' must have '\' in regexp.
    Free: Web Programming Courses HTML, CSS, Flash
    Web Programming: AJAX Course and PHP-MySQL Course video Lessons
    Good JavaScript and jQuery course for beginners

  3. #3
    Community Advisor silver trophybronze trophy
    dresden_phoenix's Avatar
    Join Date
    Jun 2008
    Location
    Madison, WI
    Posts
    2,819
    Mentioned
    34 Post(s)
    Tagged
    2 Thread(s)
    Thanks Mar Plo.

    For the sake of any others who may be searching for this topic i found that you need to DOUBLE ESCAPE when utilizing back references within the regex string.: "/<([a-z]+)>[^<]*<\/\\1>/" works as well!

  4. #4
    @php.net Salathe's Avatar
    Join Date
    Dec 2004
    Location
    Edinburgh
    Posts
    1,397
    Mentioned
    65 Post(s)
    Tagged
    0 Thread(s)
    Within double-quoted strings, \1 has special meaning. It is interpreted as an escape sequence referencing an ASCII character by an octal (base 8) number. This is the SOH or start of heading character (see Ascii table), which is then passed to PCRE (the regex library being used) as that character.

    See the strings documentation which details the different kinds of escape sequences available in double-quoted strings.

    As you saw, to prevent PCRE being passed the wrong thing, you can escape the backslash character to prevent PHP interpreting it as the special octal notation. The other option would be to use a single-quoted string, which does not interpret this style of escape sequence.
    Salathe
    Software Developer and PHP Manual Author.


Bookmarks

Posting Permissions

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