Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 12 of 12
  1. #1
    New Coder
    Join Date
    Oct 2009
    Posts
    51
    Thanks
    6
    Thanked 0 Times in 0 Posts

    For Loop using Stripos

    Hello,

    I have some Wordpress code that goes through the source of a page and locates the first instance of <img to use as a thumbnail in other sections of the site. On a few pages, a header.gif image is actually teh first instance, so I'd want to get the second instance in these cases. Here's the existing code:
    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 insert a For Loop (or something as good) to test for "header.gif" and move to the next image in these cases?

  • #2
    Senior Coder angst's Avatar
    Join Date
    Apr 2004
    Location
    Toronto, Ontario
    Posts
    2,114
    Thanks
    15
    Thanked 122 Times in 122 Posts
    this might not be the best way, but it works:

    PHP Code:
    $content "<head></head><body><html><p>some text...</p><img src=\"header.gif\" />blah blah blah<p><img src=\"someImage.jpg\" /></body></html>";
    $image '';
    $x stripos($content'<img');

    while(
    $i <= 2){
        if (
    $x !== false) {
            
    $x stripos($content'src="'$x);
            if (
    $x !== false) {
                
    $x += 5;
                
    $y strpos($content'"'$x);
                
    $image substr($content$x$y-$x);
                if(
    $image !== "header.gif"){
                    break;
                }
            }
        }
    }
    echo 
    $image

  • #3
    Regular Coder
    Join Date
    Nov 2009
    Location
    Hamilton, New Zealand
    Posts
    126
    Thanks
    0
    Thanked 17 Times in 17 Posts
    Just add another line to search for <img> againt after the first search. I added the extra line to your script, which you can see below.
    PHP Code:
    <?php
    // Image extraction
            
    $image '';
            
    $x stripos($content'<img');
            
    $x stripos($content'<img'$x+1); // This is the added line
            
    if ($x !== false) {
                
    $x stripos($content'src="'$x);
                if (
    $x !== false) {
                    
    $x += 5;
                    
    $y strpos($content'"'$x);
                    
    $image substr($content$x$y-$x);
                }
            }
    ?>
    Affordable Web Design (New Zealand Based)
    Internet Marketing Guru
    PHP/mySQL Expert
    -------------------------------------------

  • #4
    Regular Coder
    Join Date
    Dec 2009
    Location
    UK
    Posts
    495
    Thanks
    0
    Thanked 58 Times in 58 Posts
    Personally I'd use a regular expression to extract the src from the image
    PHP Code:
    preg_match('/<img[^>]+\bsrc="([^"]+\.jpg)"[^>]>/'$content$matches);
    $image $matches[1]; 
    My site: JayGilford.com
    Resources:
    PHP Pagination Class | Getting all page links | Handling PHP Errors properly
    If you like a users help, show your appreciation with the rep and thanks buttons :)

  • #5
    New Coder
    Join Date
    Oct 2009
    Posts
    51
    Thanks
    6
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by angst View Post
    this might not be the best way, but it works:

    PHP Code:
    $content "<head></head><body><html><p>some text...</p><img src=\"header.gif\" />blah blah blah<p><img src=\"someImage.jpg\" /></body></html>";
    $image '';
    $x stripos($content'<img');

    while(
    $i <= 2){
        if (
    $x !== false) {
            
    $x stripos($content'src="'$x);
            if (
    $x !== false) {
                
    $x += 5;
                
    $y strpos($content'"'$x);
                
    $image substr($content$x$y-$x);
                if(
    $image !== "header.gif"){
                    break;
                }
            }
        }
    }
    echo 
    $image
    This code doesn't work for me - it crashes PHP

  • #6
    New Coder
    Join Date
    Oct 2009
    Posts
    51
    Thanks
    6
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Rebbu View Post
    Just add another line to search for <img> againt after the first search. I added the extra line to your script, which you can see below.
    I don't follow this - where exactly is your code comparing to header.gif??

  • #7
    Senior Coder angst's Avatar
    Join Date
    Apr 2004
    Location
    Toronto, Ontario
    Posts
    2,114
    Thanks
    15
    Thanked 122 Times in 122 Posts
    ah, it just needs the counter to increment.

    PHP Code:
    $content "<head></head><body><html><p>some text...</p><img src=\"header.gif\" />blah blah blah<p><img src=\"someImage.jpg\" /></body></html>";
    $image '';
    $x stripos($content'<img');
    $i 0;
    while(
    $i <= 2){
            
    $i++;
        if (
    $x !== false) {
            
    $x stripos($content'src="'$x);
            if (
    $x !== false) {
                
    $x += 5;
                
    $y strpos($content'"'$x);
                
    $image substr($content$x$y-$x);
                if(
    $image !== "header.gif"){
                    break;
                }
            }
        }
    }
    echo 
    $image

  • Users who have thanked angst for this post:

    billatl (01-11-2010)

  • #8
    New Coder
    Join Date
    Oct 2009
    Posts
    51
    Thanks
    6
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by JAY6390 View Post
    Personally I'd use a regular expression to extract the src from the image
    PHP Code:
    preg_match('/<img[^>]+\bsrc="([^"]+\.jpg)"[^>]>/'$content$matches);
    $image $matches[1]; 
    I don't have a lot of experience with Regular Expressions. I understand your concept of matching the string form the src tag - but I'm not clear how to put this in the For Loop

  • #9
    Regular Coder
    Join Date
    Dec 2009
    Location
    UK
    Posts
    495
    Thanks
    0
    Thanked 58 Times in 58 Posts
    You don't need the for loop. You literally just need that and your $content
    My site: JayGilford.com
    Resources:
    PHP Pagination Class | Getting all page links | Handling PHP Errors properly
    If you like a users help, show your appreciation with the rep and thanks buttons :)

  • #10
    Regular Coder
    Join Date
    Nov 2009
    Location
    Hamilton, New Zealand
    Posts
    126
    Thanks
    0
    Thanked 17 Times in 17 Posts
    Quote Originally Posted by billatl View Post
    I don't follow this - where exactly is your code comparing to header.gif??
    Uh, compare the script you posted with the one I did. You'll notice the one I posted has an additional line...
    Affordable Web Design (New Zealand Based)
    Internet Marketing Guru
    PHP/mySQL Expert
    -------------------------------------------

  • #11
    New Coder
    Join Date
    Oct 2009
    Posts
    51
    Thanks
    6
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Rebbu View Post
    Uh, compare the script you posted with the one I did. You'll notice the one I posted has an additional line...
    Of course I see the extra line. Perhaps I should re-phrase my statement: "I don't understand your code". Your code:

    Code:
    $x = stripos($content, '<img', $x+1); // This is the added line
    If $x equals the position of "<img" in the string, how does adding 1 to $x compare it to header.gif and account for the fact that some posts have header.gif and other don't??

  • #12
    Regular Coder
    Join Date
    Nov 2009
    Location
    Hamilton, New Zealand
    Posts
    126
    Thanks
    0
    Thanked 17 Times in 17 Posts
    Oh my apologies, I didn't read the original post properly. I didn't realize you wanted to skip the first instance of "<img" only IF the src for that img was header.gif.

    Could do something like this:

    PHP Code:
    $headsrc stripos("header.gif");
    if (
    $headsrc===false){
      
    $x stripos($content'<img');
    }else{
      
    $first stripos($content'<img');
      
    $second stripos($content'<img'$first 1);
      if ((
    $headsrc $first) && ($headsrc <$second)){//there between the few tags, so it must be in the first img (of course there are problems with this)
        
    $x $second;
      }else{
        
    $x $first;
      }

    Last edited by Rebbu; 01-12-2010 at 01:21 AM.
    Affordable Web Design (New Zealand Based)
    Internet Marketing Guru
    PHP/mySQL Expert
    -------------------------------------------


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •