2

I have the results from a database that contains images and text. I would like to remove the first image.

Example:

$string = 'This is some text and images 
    <img src="http://linktoimage" border="0">,
    more text and <img src="http://linktoanotherimage"> and so on';

I would like to remove the first image, it is not always the same url.

Thanks for any help.

hakre
  • 193,403
  • 52
  • 435
  • 836
john tully
  • 299
  • 1
  • 8
  • 14
  • Because your string is in fact regular, you may accomplish this with a simple regular expression. Please try it, and post what you've written so far if/when you get stuck. – user229044 Jan 04 '12 at 19:21

6 Answers6

4
$string = 'This is some text and images 
    <img src="http://linktoimage" border="0">,
    more text and <img src="http://linktoanotherimage"> and so on';

print preg_replace('/<img(.*)>/i','',$string,1);

The above should return

This is some text and images 
,
    more text and <img src="http://linktoanotherimage"> and so on

Assuming you know it'll be prefixed by spaces and a line break, and suffixed by a comma and line break (and you want to remove these, too), you can do

print preg_replace("/\n    <img(.*)>\,\n    /i",'',$string,1);

Which will give you

This is some text and images more text and <img src="http://linktoanotherimage"> and so on
kba
  • 19,333
  • 5
  • 62
  • 89
3

There was a great answer on another Thread

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

    $post_dom = str_get_dom($html);

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

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

    return null;
}

You can do it via Regex expressions however regex isn't really suited for this.

Howdy_McGee
  • 10,422
  • 29
  • 111
  • 186
John Mitchell
  • 9,653
  • 9
  • 57
  • 91
  • 1
    On the contrary, I think html parsers are overkill for such a simple task. – Slavic Jan 04 '12 at 19:30
  • I agree with Slavic, I think regex is perfectly suitable for this. [No, HTML is not a regular language](http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags), but for something like this I think it's a lot more suitable than loading a huge HTML parser. – kba Jan 04 '12 at 19:36
  • The problem with it is getting the correct regex string, remember that its not always https://XXX/img.jpg, you could also end up parsing images which are not images for example it may be overkill but if you cannot validate or scrub the input it may be the best way ;) – John Mitchell Jan 04 '12 at 19:43
  • Nope. Your string won't match the regex. () – Slavic Jan 04 '12 at 19:50
1
$var = 'This is some text and images 
       <img src="http://linktoimage" border="0">, 
       more text and <img src="http://linktoanotherimage"> and so on';

echo preg_replace('/<img.*?>/', '123', $var, 1);

This should do it. ? in the regex is to make it ungreedy.

osoner
  • 2,425
  • 1
  • 15
  • 13
0

This code works for me

$html is your html content variable from which you want to remove first image tag.

$str = preg_replace('/(<img[^>]+>)/i','',$html,1);  
Talha
  • 41
  • 7
0

Being a RegEx newbie, I tend to avoid it and use something like:

$i = strpos($string,'<img');
if ($i !== false) {
    $j = strpos($string, '>', $i);
    if ($j !== false) $string = substr($string,0,$i) . substr($string,$j);
}

Probably should be $i-1 and/or $j+1 - I never remember exactly what it should be.

Matt H
  • 6,422
  • 2
  • 28
  • 32
  • shed the fear, embrace the regex - you'll be happier in the end. – KevinDTimm Jan 04 '12 at 19:27
  • I know @KevinDTimm! Is there a RegEx Support Group out there for me, with, like, cookies and folding chairs? :-) – Matt H Jan 04 '12 at 19:30
  • Sorry, your code won't work if there are any other tags in the beginning. The condition ws to remove first img tag. And it's not the only downside. – Slavic Jan 04 '12 at 19:38
0

Finally nailed it down:

preg_replace('/<img[\S\s]*?>/i','',$txt,1)

This one also works for cases when you might have:

$string = 'This is some text and images 
    <img 
        src="http://linktoimage" border="0">,
    more text and <img src="http://linktoanotherimage"> and so on';

(a new line character in the tag.)

Slavic
  • 1,891
  • 2
  • 16
  • 27