Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 02-18-2013, 09:11 AM   PM User | #1
Anishgiri
Regular Coder

 
Join Date: May 2010
Posts: 173
Thanks: 0
Thanked 0 Times in 0 Posts
Anishgiri is an unknown quantity at this point
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.
Anishgiri is offline   Reply With Quote
Old 02-18-2013, 02:28 PM   PM User | #2
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,635
Thanks: 4
Thanked 2,448 Times in 2,417 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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.
__________________
As of PHP 5.5, the MySQL library has been officially deprecated. It is recommended to move to either MySQLi or PDO libraries for your mysql connectivity. See here for help choosing which interface you prefer: http://php.net/manual/en/mysqlinfo.api.choosing.php
Fou-Lu is offline   Reply With Quote
Old 02-18-2013, 02:31 PM   PM User | #3
mlseim
Master Coder

 
mlseim's Avatar
 
Join Date: Jun 2003
Location: Cottage Grove, Minnesota
Posts: 9,045
Thanks: 8
Thanked 1,029 Times in 1,020 Posts
mlseim has a spectacular aura aboutmlseim has a spectacular aura aboutmlseim has a spectacular aura about
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.
mlseim is offline   Reply With Quote
Old 02-18-2013, 03:02 PM   PM User | #4
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,635
Thanks: 4
Thanked 2,448 Times in 2,417 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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.
__________________
As of PHP 5.5, the MySQL library has been officially deprecated. It is recommended to move to either MySQLi or PDO libraries for your mysql connectivity. See here for help choosing which interface you prefer: http://php.net/manual/en/mysqlinfo.api.choosing.php
Fou-Lu is offline   Reply With Quote
Old 02-18-2013, 07:40 PM   PM User | #5
mlseim
Master Coder

 
mlseim's Avatar
 
Join Date: Jun 2003
Location: Cottage Grove, Minnesota
Posts: 9,045
Thanks: 8
Thanked 1,029 Times in 1,020 Posts
mlseim has a spectacular aura aboutmlseim has a spectacular aura aboutmlseim has a spectacular aura about
I am an integer. I am an integer. I am an integer. 1 lump please. I am an integer. I am an integer.
mlseim is offline   Reply With Quote
Old 02-19-2013, 12:57 AM   PM User | #6
Anishgiri
Regular Coder

 
Join Date: May 2010
Posts: 173
Thanks: 0
Thanked 0 Times in 0 Posts
Anishgiri is an unknown quantity at this point
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..
Anishgiri is offline   Reply With Quote
Old 02-19-2013, 01:33 AM   PM User | #7
mlseim
Master Coder

 
mlseim's Avatar
 
Join Date: Jun 2003
Location: Cottage Grove, Minnesota
Posts: 9,045
Thanks: 8
Thanked 1,029 Times in 1,020 Posts
mlseim has a spectacular aura aboutmlseim has a spectacular aura aboutmlseim has a spectacular aura about
I think that if you use a rich text editor (made for PHP), it will have that feature already?
mlseim is offline   Reply With Quote
Old 02-19-2013, 09:37 AM   PM User | #8
GadgetGuy
New to the CF scene

 
Join Date: Feb 2013
Location: Delta, BC, Canada
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
GadgetGuy is an unknown quantity at this point
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 !
GadgetGuy is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 10:00 AM.


Advertisement
Log in to turn off these ads.