开发者

How can i get src alone from Image tag using php? [duplicate]

This question already has answers here: Closed 11 years ago.

Possible Duplicate:

How to extract img src, title and alt from html using php?

<img width="200" height="300" src="http://runapvo.apivo.com/files/2011/06/2444-200x300.jpg" class="attachment-medium" alt=开发者_Python百科"2444" title="2444" />

From this image tag I need to take src alone. How can I do this? Is there any predefined functions available?...


Load it into a DOM tree with loadHtml and do an xpath on it, or directly traverse it if the structure is always the same.

See How to extract img src, title and alt from html using php?


Assuming that you are displaying the image on your page

You should use an html dom parser. This will allow you to parse your own page. Although I am tempted to ask why you would want to do this in php of all languages? If you simply need the src on a static page, that shouldn't change so there is no need to call php everytime you want to get its src. If it is dynamic (as in defined by a php variable) you should still be able to access that variable.

Assuming the page does not belong to you

You are going to want to scrape the page using regex and php, and this pretty much answers how.

Side Note:

If it is not necessary to do it in php I would suggest passing the work onto the client using the following javascript function:

document.getElementById("MyIMG").src

The only reason this might not be favorable is if the server needs that src, the client won't have js (ie a phone browser), or the client is so bad running extra javascript might crash it :)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜