且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

用php获取字符串中的第一个图像

更新时间:2023-02-22 11:32:32

如果您只需要第一个源标记, preg_match 应该代替 preg_match_all ,这对你有用吗?

If you only need the first source tag, preg_match should do instead of preg_match_all, does this work for you?

<?php
    $texthtml = 'Who is Sara Bareilles on Sing Off<br>
    <img alt="Sara" title="Sara" src="475993565.jpg"/><br>
    <img alt="Sara" title="Sara two" src="475993434343434.jpg"/><br>';
    preg_match('/<img.+src=[\'"](?P<src>.+?)[\'"].*>/i', $texthtml, $image);
    echo $image['src'];
?>