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.
精彩评论