如何使用正则表达式获取图像的alt标记之间的值
我有<img class="sprite-ratings" alt="3 of 5 stars" src="http://c1.tacdn.com/img2/x.gif">
现在我要3星中的3星
我想在服务器端。
有谁可以帮助我?
答案 0 :(得分:8)
此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 >
答案 1 :(得分:2)
$res = preg_match("/<img[^>]+alt=\"([^>]*)\"[^>]*>/iU", $yourstring, $matches);
if ($res) {
echo "the alternative text is: " . $matches[1];
}