1

正規表現を使用して画像の alt タグ間の値を取得するにはどうすればよいですか<img class="sprite-ratings" alt="3 of 5 stars" src="http://c1.tacdn.com/img2/x.gif"> 。誰でも私を助けることができますか?

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 に答える