How to get number of image file name and image file name from php var ?
I have php var string like this
<p>aas sss ddd fff g gh ggg hhhh</p><p style="text-align: center; "><img src="threads_image/2rU4Va7c05b3f200c8cb893126fc7622b63f477ORZWM38.png" style="width: 240px;"></p><p style="text-align: left;"><img src="threads_image/2rU4V9cbdefa8e38d5457c8b1fc7ccb77c9dc30YQJUI9N.gif" style="width: 300px;"></p><p style="text-align: left;"><br></p><p style="text-align: left;"><img src="threads_image/2rU4V89e0e28ad1aab7c870d0df6380087ed378IGAN8BX.jpg" style="width: 794px;"><br></p>
So i want to get number of image file and image file name. how can i do that ?
I don't seen php function that can do that ?
答案 0 :(得分:0)
(不是我)。
如果您不想使用正则表达式(或任何非标准PHP组件),使用内置DOMDocument类的合理解决方案如下:
<?php
$doc = new DOMDocument();
$doc->loadHTML('<img src="http://example.com/img/image.jpg" ... />');
$imageTags = $doc->getElementsByTagName('img');
foreach($imageTags as $tag) {
echo $tag->getAttribute('src');
}
?>
答案 1 :(得分:0)
您可以使用正则表达式查找图像名称。这个正则表达式应该没问题:src=\"threads_image\/([^"]+)
。您可以像这样使用它:
preg_match_all('/<img src=\"threads_image\/([^"]+)/', $html , $matches);
$matches[1]
现在是图片名称列表。
C:\Users\Thomas\Projects\stackoverflow\40615130\index.php:4:
array (size=2)
0 =>
array (size=3)
0 => string '<img src="threads_image/2rU4Va7c05b3f200c8cb893126fc7622b63f477ORZWM38.png' (length=74)
1 => string '<img src="threads_image/2rU4V9cbdefa8e38d5457c8b1fc7ccb77c9dc30YQJUI9N.gif' (length=74)
2 => string '<img src="threads_image/2rU4V89e0e28ad1aab7c870d0df6380087ed378IGAN8BX.jpg' (length=74)
1 =>
array (size=3)
0 => string '2rU4Va7c05b3f200c8cb893126fc7622b63f477ORZWM38.png' (length=50)
1 => string '2rU4V9cbdefa8e38d5457c8b1fc7ccb77c9dc30YQJUI9N.gif' (length=50)
2 => string '2rU4V89e0e28ad1aab7c870d0df6380087ed378IGAN8BX.jpg' (length=50)
请注意,这仅适用于所有图片共享目录threads_image
。