Tuesday, 15 February 2011

Why does my Regex (in php) not work? -



Why does my Regex (in php) not work? -

$data html text contains lot of anchor texts (links)

$regex = '/\b<a/i'; $data = '<a href="#">test</a> <a href="#">test 2</a>'; preg_match_all($regex, $data, $matches);

returns nothing, $data has 2 <a's.

$regex = '/\b</i';

returns lots of '<' expected

$regex = '/\ba/i';

returns lots of 'a' expected, but

$regex = '/\b<a/i';

returns nothing.

why that?

besides not using dom parser, using \b problem here, because matches transition between "word-like" character , that's not, i.e.

(?<=\w)(?=\w)|(?<=\w)(?=\w)

in case, if < preceded letter \b match, e.g.:

foo<a

perhaps improve look this:

'/(?<=\s|>)<a/i'

or, this:

'/<a/i'

php regex

No comments:

Post a Comment