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.

$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?

Hi,
Try the dfollowing variant I tested it and it works:

$a="<em>some text goes here.</em>dshashd xxc dsfa";
preg_match('#\\<([a-z]+)\\>[^\\<]*\\<\\/\\1\\>#i', $a, $match); 
echo $match[1] ? $match[1]: "nope";

The ‘<’, and ‘>’ must have ‘\’ in regexp.

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!

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.