Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/svg/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 删除WordPress内容中文件名的宽度和高度_Php_Wordpress - Fatal编程技术网

Php 删除WordPress内容中文件名的宽度和高度

Php 删除WordPress内容中文件名的宽度和高度,php,wordpress,Php,Wordpress,在WordPress中上载图像时,它通常会以imagename-123x456.jpg格式显示图像的较小尺寸版本之一,其中123x456是宽度。我想过滤内容并从所有文件名的末尾删除-123x456,以便它显示原始的高分辨率图像 我是这样想的: function replace_content($content) { $content = str_replace('-123x456.jpg', '.jpg', $content); return $content; } add_fi

在WordPress中上载图像时,它通常会以imagename-123x456.jpg格式显示图像的较小尺寸版本之一,其中123x456是宽度。我想过滤内容并从所有文件名的末尾删除-123x456,以便它显示原始的高分辨率图像

我是这样想的:

function replace_content($content) {
    $content = str_replace('-123x456.jpg', '.jpg', $content);
    return $content;
}
add_filter('the_content','replace_content');
但显然,它需要更通用,以取代所有可能的大小


另外,我不想在post editor中设置图像的大小。

要从所有图像中删除
内容()中的大小,并支持不同的图像扩展,您需要像这样使用preg\u replace

function replace_content($content) {
    $content = preg_replace('/-([^-]*(\d+)x(\d+)\.((?:png|jpeg|jpg|gif|bmp)))"/', '.${4}"', $content);
    return $content;
}
add_filter('the_content','replace_content');

谢谢@Shibi,成功了!我刚刚发现,如果图像名为
imagename something.jpg
,它将错误地输出
imagename.jpg
。您好,我编辑了代码。我现在检查url中是否也有(数字)x(数字)。