How to Extract <img src> from html using php ?

Discussion in 'PHP' started by shortcut, Oct 1, 2009.

  1. #1
    Hi

    I've this code

    
    <a href="http://google.com"><img border="0" src="http://www.google.co.in/intl/en_com/images/logo_plain.png">
    
    Code (markup):

    I want to get the src="" part of the above line and then update the entire code with


    
    <a rel="lightbox" href="http://google.com"><img border="0" src="http://www.google.co.in/intl/en_com/images/logo_plain.png">
    
    Code (markup):

    How this can be done using php ?
     
    shortcut, Oct 1, 2009 IP
  2. pixmania

    pixmania Peon

    Messages:
    229
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Not sure if this is what your looking for ?

    $find_src = preg_replace('#src="(.*?)"#','src="replace with this new image"',$find_src);
    
    echo $find_src;
    PHP:
    Anything between the two ## will be replaced with src="replace with this new image"
     
    pixmania, Oct 1, 2009 IP
  3. shortcut

    shortcut Peon

    Messages:
    116
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    No. This is not what I actually want.

    I just want to add
    to the <a href> tag immediate after the <a.
    But note that , I need to add this line only if the <a tag is for <img>

    That is

    <a href="url> <img src="pic.png"></a> - > I need to add

    <a href="url> Some text </a> -- > I don't need to add
     
    shortcut, Oct 1, 2009 IP
  4. shortcut

    shortcut Peon

    Messages:
    116
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Not really

    I want to add that line to <a> tag, not <img >

    Like

    <a rel="lightbox" href="url> <img src="pic.png"></a> - > I need to add
    Code (markup):
     
    shortcut, Oct 1, 2009 IP
  5. pixmania

    pixmania Peon

    Messages:
    229
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #5
    What effect would it have if the rel="lightbox" was added to normal text links ?

    $find_href = preg_replace('#<a href="#','<a rel="lightbox href="',$find_href);
    
    echo $find_href;
    PHP:
     
    pixmania, Oct 1, 2009 IP
  6. shortcut

    shortcut Peon

    Messages:
    116
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    No, this is not the one I wanted.

    Add rel="lightbox" only if it is for IMG tag
     
    shortcut, Oct 1, 2009 IP
  7. pixmania

    pixmania Peon

    Messages:
    229
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Download your database and do a find & replace, then star fresh writing skills. Otherwise its impossible to archive what you want.
     
    pixmania, Oct 1, 2009 IP
  8. young coder

    young coder Peon

    Messages:
    302
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    0
    #8
    maybe this

    $linkWithimg = '<a href="url"><img src="pic.png"></a>';
    if (preg_match("/<img src=/i", $linkWithimg)) {
    	$final = str_ireplace("<a href=", '<a rel="lightbox" href=', $linkWithimg);
    	echo $final;
    	}
    PHP:
     
    young coder, Oct 1, 2009 IP