簡體   English   中英

在img標簽的alt之間獲取價值

[英]getting value between alt of img tag

如何使用正則表達式獲取圖像的alt標簽之間的值,例如,我有<img class="sprite-ratings" alt="3 of 5 stars" src="http://c1.tacdn.com/img2/x.gif">現在我希望在服務器端獲得5星中的3星。 誰能幫我?

regex將匹配: '/<img.*?alt="(.*?)".*>/'

preg_match('/<img.*?alt="(.*?)".*>/',$str,$match);
echo $match[1];

輸出:

3 of 5 stars

說明:

<img  # Match the literal string 
.*?   # Match anything after (non-greedy)
alt=" # Upto the literal string
(.*?) # Capture everything after
"     # Upto the literal
.*>   # Match everything else up to the closing >
$res = preg_match("/<img[^>]+alt=\"([^>]*)\"[^>]*>/iU", $yourstring, $matches);
if ($res) {
    echo "the alternative text is: " . $matches[1];
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM