Afshin
Afshin

Reputation: 2437

php - extract text between parenthesis, single quotes and double quotes

I want to extract text between parenthesis, single quotes and double quotes. I have made it for single and double quotes, but I cant make it for the third parameter, parenthesis:

for example:

<img src="/path/to/the/file.jpg"></div>
<img src='/path/to/the/file2.png'></div>
<div style="background:url(/path/to/the/file555.gif)"></div>

I want to extract:

/path/to/the/file.jpg
/path/to/the/file2.png
/path/to/the/file555.gif

and this is the regex expression I am using:

preg_match_all('/(["\'])(.*(?:\.jpg|\.png|\.gif))\1/m', $subject, $matches)

or even this one:

preg_match_all('/(["\'])([^"\']*(?:\.jpg|\.png|\.gif))\1/m', $subject, $matches)

Upvotes: 2

Views: 1265

Answers (2)

Mayur Koshti
Mayur Koshti

Reputation: 1852

You can also use the regex:

[\"\'\(]+(\/[^\/]+\/[^\/]+\/[^\/]+\/[^\.]+\.(?:jpg|gif|png))[\)\"\']+

Upvotes: 0

vks
vks

Reputation: 67968

(?<=['"\(])[^"())\n']*?(?=[\)"'])

You can use this.See demo.

https://regex101.com/r/cD5jK1/12

$re = "/(?<=['\"\\(])[^\"()\\n']*?(?=[\\)\"'])/m"; 
$str = "<img src=\"/path/to/the/file.jpg\"></div>\n<img src='/path/to/the/file2.png'></div>\n<div style=\"background:url(/path/to/the/file555.gif)\"></div>"; 

preg_match_all($re, $str, $matches);

Upvotes: 1

Related Questions