我正在使用脚本获取第一张图像。Php在链接中获取图像名称
这是脚本。
$first_img = '';
$my1content = $row['post_content'];
$output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $my1content, $matches);
$first_img = $matches [1] [0];
if(empty($first_img)){ //Defines a default image
$first_img = "/img/default.png";
}
该脚本回声完整的图像链接,例如: http://mywebsite.com/images/thisistheimage.jpg
可以将图像链接,图像名称和图像extenction ,所以我需要得到3个结果
链接,例如:http://mywebsite.com/images/ 图片名称,例如:thisistheimage 图片扩展,例如:.jpg
请让我知道,如果它清楚,谢谢阅读。
不要使用正则表达式来解析HTML。 – Achrome
你的正则表达式不正确。一个人喜欢的多个img会被吸引到一个单一的结果中。不要使用正则表达式来处理html,除非你知道你在做什么。改用DOM。 –