PHP:Preg_match_all转义圆括号

时间:2013-04-06 13:17:00

标签: php preg-match

我有这样的动态字符串:

$string1 = '<a style="background-image: url(&quot;http://someurl.com/image.jpg&quot;);" class="thumb" title="title goes here" href="http://www.someurl.com/"></a>';

我的preg_match_all代码:

preg_match('/<a style="background-image: url((.*?));" class="thumb" title="(.*?)" href="(.*?)"><\/a>/', $string1, $matches);

echo $matches['1'];
echo $matches['2'];
echo $matches['3'];

url()括号不起作用,任何想法如何逃避?

1 个答案:

答案 0 :(得分:1)

您需要使用(转义)\

preg_match('/<a style="background-image: url\((.*?)\);" class="thumb" title="(.*?)" href=" (.*?)"><\/a>/', $string1, $matches);

作为一般规则,使用正则表达式来解析HTML页面是不可取的。见Parsing Html The Cthulhu Way。 为了更好的解决方案,这里有很多例子。

相关问题