Need help on parse error

Discussion in 'PHP' started by mike4uuu, Aug 5, 2011.

  1. #1
    I m using the code below to extract the first image from a wordpress post

    function catch_that_image() {
    global $post, $posts;
    $first_img = '';
    ob_start();
    ob_end_clean();
    $output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches);
    $first_img = $matches [1] [0];
    
    // no image found display default image instead
    if(empty($first_img)){
    $first_img = "/images/default.jpg";
    }
    $first_img = "<img src="' .  $first_img . '"  />";
    return $first_img;
    }
    
    PHP:
    <?php echo catch_that_image() ?>
    PHP:
    The output , i m getting as

    <img src='http://mysiteurl.com/wp-content/upload/2011/3.gif'  />
    PHP:
    i want to change the single (') quote to double quote (")

    Like

    <img src="http://mysiteurl.com/wp-content/upload/2011/3.gif"  />
    PHP:
    How this can be achieved ..

    Many thanks in Advance!
     
    mike4uuu, Aug 5, 2011 IP
  2. Dennis M.

    Dennis M. Active Member

    Messages:
    119
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    58
    #2
    I would take a look at PHP's preg_replace method. You can create a simple regex to find a URL with single quotes (if one exists) and then replace it with the appropriate double quotes.

    Regards,
    Dennis M.
     
    Dennis M., Aug 5, 2011 IP
  3. tulvit

    tulvit Peon

    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    You have a syntax error in
    $first_img = "<img src="' .  $first_img . '"  />";
    PHP:
    Try this, it should work:
    $first_img = '<img src="' .  $first_img . '"  />';
    PHP:
     
    tulvit, Aug 6, 2011 IP