2010-07-29 49 views
1

我努力學習REG EXP,PHP reqular表達:img標籤中提取標題

我想只從文本獲得的<img標題

文字是不同的,基本上是這樣的

<a href="http://abcde.com" target="_blank"><img src="http://abcde.com/img/aa.jpg" title="img title" id="img id" class="img class" /> 

任何指導和諮詢是感激..

+0

'expression' not'expersion' in the subject .... please :( - 或'regex'來保存一些打字和拼寫錯誤。 – 2010-07-29 08:19:14

回答

3

這將是類似的東西:

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

,但我會建議通過DOM HTML解析(DomDocumentsimplehtmldom等):

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

你會得到答案「不要使用正則表達式!」但是,如果你仍然想嘗試:

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

PS:此正則表達式假定有任何屬性沒有>title之前。如果不能假設,請這麼說。