Jump to content

Recommended Posts

hey all,

now i have tried for hours to try and figure this out and it seems so simple and now i am getting really frustrated with it. i want to take out the img source(line 7 and embed the image in my site.).

the xml looks like this

1.<channel>
2.      <item>
3.           <title>blah</title>
4.           <link>www.example.com</link>
5.           <description>
6.           <![CDATA[<p>blah blah blah blah blah</p><a href=http://link.com><img 
7.                          src= http://www.linktoimage.com/link.jpg/></a>
8.           </description>
9.</item>
10.</channel>

 

Link to comment
https://forums.phpfreaks.com/topic/243367-getting-a-img-source/
Share on other sites

Gizmo, I think that the OP was trying to distinguish that that is the start of line 7.

if you look to the left the numbers go from 6. to 8...

just a guess though.. :shrug:

 

you will want something like this..

 

$pattern = '~src=\s*http://www\.[a-zA-Z0-9_-]+\.[a-zA-Z]{2,4}/[a-zA-Z0-9_-]+\.[a-zA-Z]{3,4}~';
$subject = '<![CDATA[<p>blah blah blah blah blah</p><a href=http://link.com><img 7.src= http://www.linktoimage.com/link.jpg/></a>';
preg_match($pattern,$subject,$matches);
print $matches[0]; will contain your img source

 

 

from the actual URL you wont be alble to display an image using the url as the source..

however if you know that actual path to the image..you can use CURL

 

Huh?

 

If you have the url to an image, you can access the image.  I'm not sure I understand what you are trying to say here AyKay...

 

Of course I'm not clear on what the OP wants.

 

I thought he had an xml feed of some type and just wants to get the img url that's contained in the CDATA. 

from the actual URL you wont be alble to display an image using the url as the source..

however if you know that actual path to the image..you can use CURL

 

Huh?

 

If you have the url to an image, you can access the image.  I'm not sure I understand what you are trying to say here AyKay...

 

Of course I'm not clear on what the OP wants.

 

I thought he had an xml feed of some type and just wants to get the img url that's contained in the CDATA.

honestly the OP has confused me..

You have to be clear about what you mean by "post it on my website".  Are you trying to download the image and store it somewhere on your server, so that you can display it, or do you simply mean you want the url to the image, so that you can store that and later you will use in in an img tag?

Don't use regex.  PHP has built-in XML parsing tools available to make the job much easier. One such tool is SimpleXML and the example below shows how to loop over each <item> and display the associated image.  Have a read through the SimpleXML Examples page too.

 

$feed = simplexml_load_file('http://feeds.nationalgeographic.com/ng/photography/photo-of-the-day.atom');
foreach ($feed->channel->item as $item) {
printf('<img src="%s"><br>', $item->enclosure['url']);
}

(And here's it working online.)

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

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

Guest
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.