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

发布于 2024-12-05 10:40:04 字数 594 浏览 2 评论 0原文

我正在尝试从我的每篇文章中获取第一张图片。如果我只有一张图像,下面的代码效果很好。但如果我有多个,它会给我一个图像,但并不总是第一个。

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

$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];

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

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

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

北方的韩爷 2024-12-12 10:40:06

如果您只需要第一个源标记,则应使用 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'];
?>

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'];
?>
影子是时光的心 2024-12-12 10:40:06

不要使用正则表达式来解析 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}'>";

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

Don't use regex to parse html.
Use an html-parsing lib/class, as 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/

调妓 2024-12-12 10:40:06

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

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

为了获得更一致的结果,请使用此对象 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;
}

享受

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.

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.

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.

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

冷心人i 2024-12-12 10:40:06
    $mydoc = new DOMDocument();
    $mydoc->loadHTML($text);
    $imgs = $mydoc->getElementsByTagName('img');
    if ($imgs->length > 0) {
        $first_img = $imgs->item(0);
        print_r( $first_img->getAttribute("src") );
    }

因此 $first_img->getAttribute("src") 将打印找到的第一个 src。

    $mydoc = new DOMDocument();
    $mydoc->loadHTML($text);
    $imgs = $mydoc->getElementsByTagName('img');
    if ($imgs->length > 0) {
        $first_img = $imgs->item(0);
        print_r( $first_img->getAttribute("src") );
    }

So the $first_img->getAttribute("src") will print the frist src found.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文