Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting the first image in string with php

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;?>"/>
like image 708
MHowey Avatar asked Sep 20 '11 03:09

MHowey


3 Answers

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'];
?>
like image 99
derp Avatar answered Nov 19 '22 16:11

derp


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/

like image 22
The Mask Avatar answered Nov 19 '22 16:11

The Mask


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

like image 41
Alfonso Fernandez-Ocampo Avatar answered Nov 19 '22 15:11

Alfonso Fernandez-Ocampo