利用正規表示式取出標籤的屬性值。
function imageLongDescriptionConvert($m) {
var_dump($m);
}
$content = '<img class="abcde" style="background-color: #fefefe;" alt="abcdefghijklmnop" />';
$__content = preg_replace_callback("~(<img.*alt\\s*=\\s*[\"'])(?<alt>.*)([\"'].*>)~iUs", "imageLongDescriptionConvert", $content);
// 顯示結果
array(4) {
[0]=>
string(79) "<img class="abcde" style="background-color: #fefefe;" alt="abcdefghijklmnop" />"
[1]=>
string(59) "<img class="abcde" style="background-color: #fefefe;" alt=""
[2]=>
string(16) "abcdefghijklmnop"
[3]=>
string(4) "" />"
}