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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7479835/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 02:46:28  来源:igfitidea点击:

Getting the first image in string with php

phpimagepreg-match-all

提问by MHowey

I'm trying to get the first image from each of my posts. This code below works great if I only have one image. But if I have more then one it gives me an image but not always the first.

我试图从我的每个帖子中获取第一张图片。如果我只有一张图片,下面的代码效果很好。但是如果我有更多然后它给了我一个形象但并不总是第一个。

I really only want the first image. A lot of times the second image is a next button

我真的只想要第一张图片。很多时候第二张图片是下一个按钮

$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_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $texthtml, $matches);
$first_img = $matches [1] [0];

now I can take this "$first_img" and stick it in front of the short description

现在我可以把这个“$first_img”贴在简短的描述前面

<img alt="Sara" title="Sara" src="<?php echo $first_img;?>"/>

回答by derp

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

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

<?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'];
?>

回答by The Mask

Don't use regex to parse html. Use an html-parsing lib/class, as phpquery:

不要使用正则表达式来解析 html。使用 html 解析库/类,作为 phpquery:

require 'phpQuery-onefile.php';

$texthtml = 'Who is Sara Bareilles on Sing Off<br> 
<img alt="Sarahehe" title="Saraxd" src="475993565.jpg"/><br> 
<img alt="Sara" title="Sara two" src="475993434343434.jpg"/><br>'; 
$pq = phpQuery::newDocumentHTML($texthtml);
$img = $pq->find('img:first');
$src = $img->attr('src');
echo "<img alt='foo' title='baa' src='{$src}'>";

Download: http://code.google.com/p/phpquery/

下载:http: //code.google.com/p/phpquery/

回答by Alfonso Fernandez-Ocampo

After testing an answer from here Using regular expressions to extract the first image source from html codes?I got better results with less broken link images than the answer provided here.

从这里测试答案后使用正则表达式从 html 代码中提取第一个图像源?与此处提供的答案相比,我获得了更好的结果,链接图像更少。

While regular expressions can be good for a large variety of tasks, I find it usually falls short when parsing HTML DOM. The problem with HTML is that the structure of your document is so variable that it is hard to accurately (and by accurately I mean 100% success rate with no false positive) extract a tag.

虽然正则表达式适用于多种任务,但我发现它在解析 HTML DOM 时通常会达不到要求。HTML 的问题在于文档的结构如此多变,以至于很难准确地(准确地说,我的意思是 100% 成功率且没有误报)提取标签。

For more consistent results use this object http://simplehtmldom.sourceforge.net/which allows you to manipulate html. An example is provided in the response in the first link I posted.

要获得更一致的结果,请使用此对象http://simplehtmldom.sourceforge.net/,它允许您操作 html。我发布的第一个链接的响应中提供了一个示例。

function get_first_image($html){
require_once('SimpleHTML.class.php')

$post_html = str_get_html($html);

$first_img = $post_html->find('img', 0);

if($first_img !== null) {
    return $first_img->src';
}

return null;
}

Enjoy

享受