Php preg_匹配某些img文件类型
需要一些关于pregmatch的帮助,我想查找特定的文件类型(jpg、jpeg、png)。任何帮助都将不胜感激 守则:Php preg_匹配某些img文件类型,php,preg-match,expression,Php,Preg Match,Expression,需要一些关于pregmatch的帮助,我想查找特定的文件类型(jpg、jpeg、png)。任何帮助都将不胜感激 守则: $strmatch='^\s*(?:<p.*>)?\<a.*href="(.*)">\s*(<img.*src=[\'"].*[\'"]\s*?\/?>)[^\<]*<\/a\>\s*(.*)$'; if (preg_match("/$strmatch/sU", $content, $matches)) {
$strmatch='^\s*(?:<p.*>)?\<a.*href="(.*)">\s*(<img.*src=[\'"].*[\'"]\s*?\/?>)[^\<]*<\/a\>\s*(.*)$';
if (preg_match("/$strmatch/sU", $content, $matches)) {
$leadMatch=1;
}
$strmatch='^\s*(?:)?\\s*()[^\试试这个:
$strmatch='/^\s*(?:<p.*>)?\<a.*href="(.*)">\s*(<img[\sa-z0-9="\']+src="([a-z0-9]+\.[jpeg]{3,4})"[\sa-z0-9="\']*>)[^\<]*<\/a\>\s*(.*)$/ui';
$strmatch='/^\s*(?:)?\\s*()[^\您可以使用类似的方式来抓取图像:
<?php
$string = '<a href="index.html" class="bordered-feature-image"><img src="images/3D Logo.jpg" alt="" /></a> <img src="house.png" /> <img src="http://www.google.com/image.gif" alt="" />';
// GRAB THE IMAGES
preg_match_all('~(?<="|\')[A-Z0-9_ /]+\.(?:jpe?g|png)(?=\'|")~i', $string, $images);
$images = $images[0];
print "\nImages From Match: "; print_r($images);
$stratch='^\s*(?:)?\\s*()[^\n我已经用一个新的答案更新了我的答案,我相信它会起作用,我已经自己测试过了。别忘了升级投票并接受为正确:)如果模式字符串中有\/
,它将与调用本身中使用的/分隔符冲突,因为\/
将立即折叠为只/。您可以使用\\/在模式($STRACK变量)中,或者使用一个不同的定界符,例如:~,是否试图将整个IMG标记从HTML中拉出来,或者仅仅是SRC文件?您可能想在IMG的中间更改<代码> *>代码> >代码> [^ \ ' ] +< /代码>,这样您就不匹配“或”。作为。*.的一部分,是否可以匹配所有IMG标记,然后清除它们以排除.gif,还是首先只需要匹配非gif?
$accepted_image_types = array('jpg', 'jpeg', 'png');
preg_match_all('~(?<="|\')[A-Z0-9_ /]+\.(?:'.implode('|', $accepted_image_types).')(?=\'|")~i', $string, $images);
$images = $images[0];
print "\nImages Pulled In From Array: "; print_r($images);
~ (?<="|\') [A-Z0-9_ /]+ \. (?:jpe?g|png) (?=\'|") ~i
^ ^ ^ ^ ^
1 2 3 4 5