Redbox
Redbox

Reputation: 1477

PHP: Preg_match_all escape round brackets

i have dynamic string like this:

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

my preg_match_all code:

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

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

the url() bracket is not working, any idea how to escape that?

Upvotes: 1

Views: 2196

Answers (1)

Amirshk
Amirshk

Reputation: 8258

You need to escape the (,) with \:

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

As a general rule, using regex to parse an HTML page isn't the way to go. see Parsing Html The Cthulhu Way. There are plenty of examples here on SO for better solutions.

Upvotes: 1

Related Questions