Hi there,

I am building a blog at the moment i have a amazing script that search a post for the images in it. So i can just setup a post template, call the images that are in the post and go. In this way i put the images unedited and unresized in the post and the template will make everything nice and smooth.:)

The thing is that the function works great, it produces a nice image with the image tags. Only the thing is:

I don't want the whole image wrapped up in a image tag,
i want only the src of it, i mean the url of the image.

I searched a lot but couldn't find a solution so thats why this posts excists.

Here is the function.

function getImage($num) {
global $more;
$more = 1;
$link = get_permalink();
$content = get_the_content();
$count = substr_count($content, '<img');
$start = 0;
for($i=1;$i<=$count;$i++) {
$imgBeg = strpos($content, '<img', $start);
$post = substr($content, $imgBeg);
$imgEnd = strpos($post, '>');
$postOutput = substr($post, 0, $imgEnd+1);
$postOutput = preg_replace('/width="([0-9]*)" height="([0-9]*)"/', '',$postOutput);;
$image[$i] = $postOutput;
$start=$imgEnd+1;
}
if(stristr($image[$num],'<img')) { echo '<a href="'.$link.'">'.$image[$num]."</a>"; }
$more = 0;
}

I just call the images with

<?php getImageUrl('1'); ?>

or

<?php getImageUrl('2'); ?>

etc

This will produce a in my case

<img class="a" title="htc-pure-1" src="http://www.site.com/wp-content/uploads/2010/03/htc-pure-1.jpg" alt="">

What i want is just

http://www.site.com/wp-content/uploads/2010/03/htc-pure-1.jpg

Onybody got a clue?

Recommended Answers

All 5 Replies

As well explained your post was I didn't exactly get the problem. From my understanding you want to use regex to extract links from html input. If that is correct then the following would be your solution.

<?php
$html_input='<img class="a" title="htc-pure-1" src="http://www.site.com/wp-content/uploads/2010/03/htc-pure-1.jpg" alt="">';

preg_match_all('@\<img[^\>]+src\="([^"]+)"[^\>]+\>@Uis',$html_input,$output);

$output=$output[1];
print_r($output);

Well $output is an array and if you intend to only ever have 1 match then use the following.

<?php
$html_input='<img class="a" title="htc-pure-1" src="http://www.site.com/wp-content/uploads/2010/03/htc-pure-1.jpg" alt="">';

preg_match_all('@\<img[^\>]+src\="([^"]+)"[^\>]+\>@Uis',$html_input,$output);

$output=$output[1];
echo $output[0];

Got it,

i used implode, is this a good way to go with?

$html_input='<img class="a" title="htc-pure-1" src="http://www.site.com/wp-content/uploads/2010/03/htc-pure-1.jpg" alt="">';

preg_match_all('@\<img[^\>]+src\="([^"]+)"[^\>]+\>@Uis',$html_input,$output);

$output=$output[1];
$urlonly= implode(',', $output);
echo $urlonly;

i will use yours instead for the better preformence! thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.