php - 在括号,单引号和双引号之间提取文本

时间:2022-09-13 00:05:34

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)

2 个解决方案

#1


1  

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

You can use this.See demo.

你可以使用它。参见演示。

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

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);

#2


0  

You can also use the regex:

你也可以使用正则表达式:

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

#1


1  

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

You can use this.See demo.

你可以使用它。参见演示。

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

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);

#2


0  

You can also use the regex:

你也可以使用正则表达式:

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