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

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="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 :)

链接地址: http://www.djcxy.com/p/42160.html

上一篇: HTML属性的长度是否有限制?

下一篇: 我怎样才能从图像标签使用PHP单独src?