1

如何使用正则表达式在图像的 alt 标记之间获取值,例如,我<img class="sprite-ratings" alt="3 of 5 stars" src="http://c1.tacdn.com/img2/x.gif"> 现在想要 5 颗星中的 3 颗,我想要它在服务器端。谁能帮我?

4

2 回答 2

9

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 >
于 2012-11-26T12:40:07.040 回答
2
$res = preg_match("/<img[^>]+alt=\"([^>]*)\"[^>]*>/iU", $yourstring, $matches);
if ($res) {
    echo "the alternative text is: " . $matches[1];
}
于 2012-11-26T12:38:55.620 回答