• Resolved d4jaj1

    (@d4jaj1)


    Hello,

    I have a plugin that gets the first image from the content of a post. Here’s the code:

    // Image extraction
            $image = '';
            $x = stripos($content, '<img');
    
            if ($x !== false) {
                $x = stripos($content, 'src="', $x);
                if ($x !== false) {
                    $x += 5;
                    $y = strpos($content, '"', $x);
                    $image = substr($content, $x, $y-$x);
                }
            }

    The “$x = stripos($content, ‘<img’);” finds the first instance of <img and returns the result. I would like to modify this to get the 2nd image if the first image found is “header.gif”.

    Any suggestions on how to modify the code to accomplish this?

Viewing 1 replies (of 1 total)
  • // Image extraction
            $image = '';
            $x = stripos($content, '<img');
    
            if ($x !== false) {
                $x = stripos($content, 'src="', $x);
                if ($x !== false) {
                    $x += 5;
                    $y = strpos($content, '"', $x);
                    $image = substr($content, $x, $y-$x);
                }
            }
    // compare the result with 'header.gif' - you need the full path, i.e. what your first image extraction results;
    if($image == 'header.gif') :
    // shorten $content into $content2, incase you need $content later
    $content2 = substr($content, $y);
    // Image extraction second time:
            $image = '';
            $x = stripos($content2, '<img');
    
            if ($x !== false) {
                $x = stripos($content2, 'src="', $x);
                if ($x !== false) {
                    $x += 5;
                    $y = strpos($content2, '"', $x);
                    $image = substr($content2, $x, $y-$x);
                }
            }
    endif;

    untested ??

Viewing 1 replies (of 1 total)
  • The topic ‘Looping Through Post Content w/stripos’ is closed to new replies.