2013-04-06 82 views
1

我有动态字符串是这样的:PHP:Preg_match_all逃脱圆括号

$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']; 

的网址()括号不工作,任何想法如何逃脱?

+2

'\\'是典型的转义字符。 – Jon 2013-04-06 13:18:27

+0

[括号中的PHP正则表达式]可能的重复(http://stackoverflow.com/questions/8220180/php-regex-with-parentheses) – mario 2013-04-06 13:19:59

回答

1

你需要躲避()\

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

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

+0

这项工作,谢谢 – Redbox 2013-04-06 13:22:55