使用PHP preg_match_all提取特定链接

使用PHP preg_match_all提取特定链接,php,curl,preg-match-all,Php,Curl,Preg Match All,我有一个包含 <img width="10" height="12" scr="https://www.site.com/yughggcfgh"> <img width="11" height="15" scr="https://www.site.com/yughggcfghcvbcvb"> <img width="10" height="12" scr="https://www.site.com/a.jpg"> <img width="10" hei

我有一个包含

 <img width="10" height="12" scr="https://www.site.com/yughggcfgh">
<img width="11" height="15" scr="https://www.site.com/yughggcfghcvbcvb">
<img width="10" height="12" scr="https://www.site.com/a.jpg">
<img width="10" height="12" scr="https://www.site.com/b.gif">

我想提取数组中没有扩展名的图像路径,
输出必须如下所示

ari[1]= <img width="10" height="12" scr="https://www.site.com/yughggcfgh">
ari[2]= <img width="11" height="15" scr="https://www.site.com/yughggcfghcvbcvb"> 
ari[1]=
ari[2]=

正则表达式可能不是适合这项工作的工具,但现在您可以

您应该能够通过以下方式实现您的目标:


preg_match_all('/src=“.+?(?正则表达式可能不是该作业的合适工具,但现在您可以

您应该能够通过以下方式实现您的目标:

preg_match_all('/src=“.+?(?您真的应该使用或使用一些html解析器而不是regex,下面是一个示例:

<?php 
$somesource='<img width="10" height="12" src="https://www.site.com/yughggcfgh">
<img width="11" height="15" src="https://www.site.com/yughggcfghcvbcvb">
<img width="10" height="12" src="https://www.site.com/a.jpg">
<img width="10" height="12" src="https://www.site.com/b.gif">';

$xml = new DOMDocument();
@$xml->loadHTML($somesource);
foreach($xml->getElementsByTagName('img') as $img) {
    if(substr($img->getAttribute('src'),-4,1)!='.'){
        $image[] = $img->getAttribute('src');
    }
}

print_r($image);

Array
(
    [0] => https://www.site.com/yughggcfgh
    [1] => https://www.site.com/yughggcfghcvbcvb
)

?>

您真的应该使用或使用一些html解析器,而不是regex,以下是一个示例:

<?php 
$somesource='<img width="10" height="12" src="https://www.site.com/yughggcfgh">
<img width="11" height="15" src="https://www.site.com/yughggcfghcvbcvb">
<img width="10" height="12" src="https://www.site.com/a.jpg">
<img width="10" height="12" src="https://www.site.com/b.gif">';

$xml = new DOMDocument();
@$xml->loadHTML($somesource);
foreach($xml->getElementsByTagName('img') as $img) {
    if(substr($img->getAttribute('src'),-4,1)!='.'){
        $image[] = $img->getAttribute('src');
    }
}

print_r($image);

Array
(
    [0] => https://www.site.com/yughggcfgh
    [1] => https://www.site.com/yughggcfghcvbcvb
)

?>


我想您可能有打字错误
scr=
src=
可能重复的我想你的拼写错误是
scr=
src=
error:Warning:preg_match()[function.preg match]:第19行C:\xampp\htdocs\curl\index.php中的分隔符不能是字母数字或反斜杠错误:Warning:preg_match()[function.preg match]:第19行的C:\xampp\htdocs\curl\index.php中的分隔符不能是字母数字或反斜杠。对于图像扩展的构成可能会有更严格的限制。我注意到这是一个示例,我在这里不是要完成PEEP脚本,而是只给出构建appon的示例;pThanks它可以工作!domDocument可以在每台php服务器上工作吗???@Alfredfrancis YeIt’任何使用PHP5的服务器都可以。别忘了浏览文档。@Lawrencerone:嗯……很公平;)可能对图像扩展的构成有更严格的限制。我注意到这是一个例子,我不是来完成peeps脚本的,只是给出构建appon的例子;pThanks它可以工作!domDocument可以在每台PHP服务器上工作吗???@Alfredfricans是的,任何使用PHP5的服务器都可以。别忘了浏览文档。@Lawrencerone:好的…很公平;)