Php 如何在getimagesize中预存过滤器

Php 如何在getimagesize中预存过滤器,php,Php,我想发布内容所有img标签添加高度大小 我不能preg\u match\u all。因为img标记数组不是 preg_filter-执行正则表达式搜索和替换 getimagesize(IMGURL)[1]是高度大小的pixcel $2是img标签src URL。bat字符串不在$2 preg_filter('/<img(.+?)src="(.+?)"(.+?)>/i', "<img$1src=\"$2\"$3 height=\"".getimagesiz

我想发布内容所有img标签添加高度大小

我不能
preg\u match\u all
。因为img标记数组不是

preg_filter
-执行正则表达式搜索和替换

getimagesize(IMGURL)[1]
是高度大小的pixcel

$2
是img标签src URL。bat字符串不在
$2

preg_filter('/<img(.+?)src="(.+?)"(.+?)>/i',
            "<img$1src=\"$2\"$3 height=\"".getimagesize("{$2}")[1]."\">",
            $row['post_content']);

// <img src="/abc/a.jpg" height="">


preg_filter('/<img(.+?)src="(.+?)"(.+?)>/i',
            "<img$1src=\"$2\"$3 height=\"".getimagesize(."{$2}".)[1]."\">",
            $row['post_content']);

// PHP Parse error:  syntax error, unexpected '.' in /var/www/a/a.php line 22

您可以改为使用preg\u replace\u callback()

function setheight($matches){
    $img_prop = getimagesize($matches[2]);
    return '<img' . $matches[1] . 'src="'.$matches[2].'" '. $matches[3] . ' height="'.$img_prop[2].'">';
}

preg_replace_callback(
            "/<img(.+?)src=\"(.+?)\"(.+?)>/i",
            "setheight",
            $row['post_content']);
函数设置高度($matches){
$img_prop=getimagesize($matches[2]);
返回“”;
}
preg_replace_回调(
“//我”,
“设定高度”,
$row['post_content']);
function setheight($matches){
    $img_prop = getimagesize($matches[2]);
    return '<img' . $matches[1] . 'src="'.$matches[2].'" '. $matches[3] . ' height="'.$img_prop[2].'">';
}

preg_replace_callback(
            "/<img(.+?)src=\"(.+?)\"(.+?)>/i",
            "setheight",
            $row['post_content']);