开发者

PHP reqular expression: extracting title of img tag

I'm trying to learn reg exp,

i want to get title of <img only from the text

the text is differ and basically look like this

开发者_JAVA技巧
<a href="http://abcde.com" target="_blank"><img src="http://abcde.com/img/aa.jpg" title="img title" id="img id" class="img class" />

any guidance and advise are appreciated..


it would be something like that:

'|<img(.*)title="(.*)"(.*)/>|Um'

but i would advise to parse html through dom (DomDocument, simplehtmldom, etc):

$doc = new DomDocument();
$doc->loadHTML($page);
$imgElement = $doc->getElementById('img id');
$title = $imgElement->getAttribute('title');


look in this so answer:

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

a lot of good answers !


You'll get the answer "Don't use regular expressions!" But if you still want to, try:

#<img[^>]+title="([^"]*)"#

PS: This regex assumes that there is no > in any attribute before title. If this may not be assumed, say so.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜