Jump to content

Function used to print <img> info, how can I grab the 'src'?


Recommended Posts

Hi gurus,


I've ran into a bit of a problem, and I can't seem to find the right search terminology to find myself a solution.. I'm not an experienced programmer in PHP by any means, I only know the basics, and how it works etc.


At the moment, I'm creating a website that displays products for sale. It uses WordPress as it's CMS, although I've added and removed bits of code all over the place to make the site work just the way I want it to.


The problem I'm having is regarding a plugin called 'CFI (Custom Field Image)', which basically allows the user to add a picture to their post as a custom field, rather than embedding it into the post data. If you go to http://claremasonjewellery.co.uk/DATABASE you can see it working on the category pages. In the 'loop', I call 'custom_field_image' which generates the dynamic <img> for each post.


What I want to do is make it so that when viewing a single post, clicking on the large image makes it full size in a separate window. However, I cannot for the life of me get JUST the 'src' part of the img info that the function creates, because the place where it's generated is embedded inside two more nested functions.


Incase you're a little lost and I'm not explaining myself well enough, I made the error of writing

$imageData = explode(" ", custom_field_image);
print $imageData[3];


to try and make happen what I want, but it instead just printed another image.


Someone please help :(

Link to comment
Share on other sites

You could try preg_match_all or maybe preg_replace to strip everything out except for src="XX" then another preg_replace to strip out src="" leaving XX, altho it can just be done with one preg_replace.


I can post the code for you. How ever, you need to tell me. Is the url inside ' or "?


Example with ':

<img src='link'>


Example with ":

<img src="link">

Link to comment
Share on other sites

Excellent! Thank you so much, I didn't think the preg_match_all() or preg_replace() functions would work with another function as their data source.. Just like the explode() didn't work. But if you think it will, and it does, then I'm uber grateful!!


The img src url is inside "s, not 's :)


P.s. If you could write out the code for me, that'd be much appreciated.. I've never used those functions before so have no idea how they work!

Link to comment
Share on other sites

$string = 'some text with image <img src="url_here">';
$new = preg_replace('~(.*)<img src="(.*)">~i','$2',$string);
echo $new;


Returns: url_here


Hope that help's. If it don't just reply back and ill try another small solution.

Link to comment
Share on other sites

Thanks for your help. That hasn't worked though, it printed out the whole of the function, not just the image part. Maybe it'd be helpful if I told you what the function prints? Writing simply:


<?php custom_field_image();?>


..returns the following:


<img src="http://localhost/wp-content/uploads/2009/02/p1030447-370x277.jpg" class="cfi alignleft" alt="Indian Summer Intertwined Bracelet" title="Indian Summer Intertwined Bracelet" />


Using the code you gave me doesn't have any effect of what it prints, it just writes out exactly the same result! I don't think you fully understood my problem though, so it's not your fault! :)


Does anyone else know what I could do?

Link to comment
Share on other sites

It's possible yes.


It's just take this for example.


$string = 'some url <img src="url_here">';


and with teh code above, it returns url_here


unfortunatly put this there:


$string = 'some url <img src="url_here" title="attribute" alt="attribute" width="110" height="110">';


it will return:


url_here" title="attribute" alt="attribute" width="110" height="110


so in other word's it's take all the code inside the src=" till "> i tried removing the end > and no hope. Ill continue trying tho.


Link to comment
Share on other sites

This thread is more than a year old. Are you sure you have something important to add to it?

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.