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 8 of 8
  1. #1
    Regular Coder
    Join Date
    May 2010
    Posts
    206
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Help Extracting Image url from Strings

    Is it possible to extract image url from texts?

    I am a string, I am a string image/logo.png I am a string, I am a string image/logo2.jpg I am a string, I am a string.

    How can I get image/logo.png and image2/logo.jpg from the strings? I think I need to use regular expression on it. But I am not good at regular expression.

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    That depends on what you are looking to take from it.
    Just the name for the image, like in image/logo.png you just want logo.png? You can use basename() function to get that. Pathinfo function may also be of use which cuts all the components apart.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #3
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,455
    Thanks
    8
    Thanked 1,084 Times in 1,075 Posts
    When you say "texts", do you mean an HTML webpage? Or do you mean a paragraph of text from a text file or database? The method used would be different for those two situations.

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Lols that's funny I thought that I am a string referred to the datatype. Couldn't figure out why it was repeated several times. I'm going back to my coffee.
    You can match that with a simple '~(?:image/[^\s]*)~' and a preg_match_all assuming they do not contain spaces.
    mlseim is getting at you cannot just extract it like that if you are html scraping. You'll need to parse for the tags that are enclosing them, but with well designed html you can use a dom parser like DomDocument or simplexml to pull it up.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #5
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,455
    Thanks
    8
    Thanked 1,084 Times in 1,075 Posts
    I am an integer. I am an integer. I am an integer. 1 lump please. I am an integer. I am an integer.

  • #6
    Regular Coder
    Join Date
    May 2010
    Posts
    206
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Pardon for not clear example what I mean is $string="blablablabla http://website.com/aa/facade.JPG blablablabla". I need to extract the url from this. I already found a regular expression.

    PHP Code:
    preg_match_all('!http://.+\.(?:jpe?g|png|gif)!Ui' $string $matches); 
    I am wondering an idea on how to create a rich text edit functionality where if you put url of image in this-->>>[IMG]image url here[/IMG], it will be parse this as an html code, which will display your image..

  • #7
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,455
    Thanks
    8
    Thanked 1,084 Times in 1,075 Posts
    I think that if you use a rich text editor (made for PHP), it will have that feature already?

  • #8
    New to the CF scene
    Join Date
    Feb 2013
    Location
    Delta, BC, Canada
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Have a look at this, it may help.

    The code below shows how to extract an 'http:'//' image based tag.
    Then display them, and modify the HTML to show you what it found.

    <?php

    $total = 0;

    $sample_HTML = "
    <html>
    <body>
    <h2>[TOTAL GOES HERE]</h2>
    <p>
    This is a sample piece of HTML<br>
    blah blah <img src='http://api.flattr.com/button/flattr-badge-large.png' [title0]><br>
    trash talk every place you look
    </p>
    <p>
    more blah blah<br>
    <img src='http://ca2.php.net/images/php.gif' [title1]><br>
    apple pear plum cherry
    </p>
    <p>
    <a href='www.google.com'>Google Chrome<img src='http://www.google.com/images/icons/product/chrome-48.png' [title2]></a>
    But are there any COOKIES !!
    </p>
    </body>
    </html>
    ";

    echo "<hr>This is what it looks like in RAW form<hr>";
    echo $sample_HTML;
    echo "<hr>";

    $new_HTML = $sample_HTML;

    preg_match_all('!http://.+\.(?:jpe?g|png|gif)!Ui' , $sample_HTML , $match);

    $total = count($match[0], true);

    echo "Total: $total<br>\n";

    for ($i = 0; $i < $total; $i++) {
    $str = $match[0][$i];
    echo "image$i: $str<br>\n";
    $new_HTML = str_replace("[title$i]", "title='$str'", $new_HTML);
    }

    echo "<hr>";

    // Show results

    $new_HTML = str_replace("[TOTAL GOES HERE]", "Total Count: $total", $new_HTML);

    $new_HTML = str_replace("</body>", "<h2>More your cursor over the images</h2></body>", $new_HTML);

    echo "<hr>This is what it looks like in PROCESSED form<hr>";
    echo $new_HTML;

    ?>

    What do you want for nothing !


  •  

    Posting Permissions

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