1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Code to Automatically detect image link

Discussion in 'PHP' started by dkin69, Feb 15, 2010.

  1. #1
    I need a small code snippet function to use on a certain database cell when calling my loop, what I need it to do is parse the code, if it finds an image link amend certain code to it, if not, return original code.

    Unfortunately this is beyond my scope of expertise so again I come to the wise programmers of digitalpoint for assistance.

    Please direct me in anyway you can.

    Thank you
     
    dkin69, Feb 15, 2010 IP
  2. SmallPotatoes

    SmallPotatoes Peon

    Messages:
    1,321
    Likes Received:
    41
    Best Answers:
    0
    Trophy Points:
    0
    #2
    If you provide a sample of what your image links look like without the added code, and a sample of what they should look like with the added code, then someone will be able to help you.
     
    SmallPotatoes, Feb 16, 2010 IP
  3. dkin69

    dkin69 Active Member

    Messages:
    644
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    60
    #3
    well the added code can be anything, the image link is just a plain image link.

    before being entered http://www.yaya.com/funny.jpg

    after <a href="http://www.yaya.com/funny">whatever</a>
     
    dkin69, Feb 16, 2010 IP
  4. dkin69

    dkin69 Active Member

    Messages:
    644
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    60
    #4
    sorry for not stating this before but there will be more text in the submission as well.

    eg

    Hi my name is danny and I love to fish http://www.dannylovestofish.com/imagefish.jpg

    output

    Hi my name is danny and I love to fish <img src="http://www.dannylovestofish.com/imagefish.jpg">
     
    dkin69, Feb 16, 2010 IP
  5. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #5
    <?php
    
    $submission = <<<TEXT
    Hi my name is danny and I love to fish http://www.dannylovestofish.com/imagefish.jpg
    TEXT;
    
    $imagelink = preg_replace("/(http:\/\/www\.[a-z0-9-]*\.[a-z\.]{2,6}\/[a-z0-9-\/]*\.[a-z]{3,5})/i", "<img src=\"$1\">", $submission);
    
    echo $imagelink;
    
    ?>
    PHP:
     
    danx10, Feb 16, 2010 IP
  6. dkin69

    dkin69 Active Member

    Messages:
    644
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    60
    #6
    It works like a charm except if im grabbing a link from somewhere like imageshack, for example.

    http://img705.imageshack.us/img705/1342/hahahalulz.jpg

    or

    http://img195.imagevenue.com/img.php?image=81164_n506933898_580553_2577_122_842lo.jpg

    does not display anything yet

    http://www.anything.com/hahaha.jpg

    would display an image.

    any thoughts?? Ive been messing with it for over an hour but I just cant understand it.
     
    Last edited: Feb 16, 2010
    dkin69, Feb 16, 2010 IP
  7. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #7
    Try this (untested):

    <?php
    
    $submission = <<<TEXT
    Hi my name is danny and I love to fish http://img195.imagevenue.com/img.php?image=81164_n506933898_580553_2577_122_842lo.jpg
    TEXT;
    
    $imagelink = preg_replace("#(http://([^/]+)[^\s]+)#", "<img src=\"$1\">", $submission);
    
    echo $imagelink;
    
    ?>
    PHP:
     
    Last edited: Feb 17, 2010
    danx10, Feb 17, 2010 IP
    dkin69 likes this.
  8. dkin69

    dkin69 Active Member

    Messages:
    644
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    60
    #8
    fantastic, thank you rep added
     
    dkin69, Feb 17, 2010 IP