tags:

views:

136

answers:

5

Ahoy there!

I can't "guess" witch syntax should I use to be able to extract the source of an image but simply the web address not the src= neither the quotes?

Here is my piece of code:

function get_all_images_src() {
    $content = get_the_content();
    preg_match_all('|src="(.*?)"|i', $content, $matches, PREG_SET_ORDER);
    foreach($matches as $path) {
     echo $path[0];
    }
}

When I use it I got this printed:

src="http://project.bechade.fr/wp-content/uploads/2009/09/mer-300x225.jpg"

And I wish to get only this:

http://project.bechade.fr/wp-content/uploads/2009/09/mer-300x225.jpg

Any idea?

Thanks for your help.

+3  A: 

$path[1] instead of $path[0]

mickael9
I'm using '$path[0]' because I want to display all the src images, I just get the first one with '$path[1]'
+5  A: 

Not exactly an answer to your question, but when parsing html, consider using a proper html parser:

foreach($html->find('img') as $element) {
  echo $element->src . '<br />';
}

See: http://simplehtmldom.sourceforge.net/

Bart Kiers
Really interesting mate ! Thanks for sharing... Makes things pretty easier
A: 
echo $path[1];

$path[0] is the full string matched. $path[1] is the first grouping.

Chadwick
Exactly my thought...
A: 

You could explode the string using " as a delimeter and then the second item in the array you get would be the right string:

$array = explode('"',$full_src);

$bit_you_want = $array[1];

Reworking your original function, it would be:

function get_all_images_src() {    
$content = get_the_content();    
preg_match_all('|src="(.*?)"|i', $content, $matches, PREG_SET_ORDER);    
foreach($matches as $path) {   
$src = explode('"', $path);     
echo $src[1];    
}
}
Can you go ahead please, I'm having hard time with PHP...
A: 

Thanks Ithcy for his right answer. I guess I've been too long to respond because he deleted it, I just don't know where his answer's gone...

So here is the one I've received by mail:

'|src="(.*?)"|i' makes no sense as a regex. try '|src="([^"]+)"|i' instead. (Which still isn't the most robust solution but is better than what you've got.)

Also, what everyone else said. You want $path[1], NOT $path[0]. You're already extracting all the src attributes into $matches[]. That has nothing to do with $path[0]. If you're not getting all of the src attributes in the text, there is a problem somewhere else in your code.

One more thing - you should use a real HTML parser for this, because img tags are not the only tags with src attributes. If you're using this code on raw HTML source, it's going to match not just but tags, etc.

— ithcy

I did everything he told me to do including using a HTML parser from Bart (2nd answer).

It works like a charm ! Thank you mate...