如何从HTML属性中的文件名中删除Wordpress图像大小?

时间:2019-05-06 10:10:13

标签: php regex wordpress

我正在使用以下代码删除Wordpress添加到媒体文件名的大小。

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');

如何更改正则表达式以仅将其应用于href属性值?

1 个答案:

答案 0 :(得分:0)

带有preg_replace()函数的正则表达式

s = '\xd1\x84\xd0\xbe\xd1\x80\xd1\x83\xd0\xbc'.encode().decode('unicode_escape') [having the bytes part between '' been gotten from a file using the open(file,"r") function, in my case]
s.encode('latin-1').decode('utf-8') [or ISO-8859-1, as it seems it's the same thing]

清除此可怕的img标签

$replaced_content = preg_replace( '#<img[^>]*?src[\s]?=[\s]?[\'"]?([^\'">]*?(https|http|\/\/)[^\'">]*?(png|jpeg|jpg|gif|bmp))[^\'" >]*?)[\'" ][^>]*?>#',
                                  '<img src="$1">', $content );

这个干净漂亮的代码

<img ttl='Ren src = https://cdn.wpbeginner.com/wp-content/uploads/2015/01/rename-on-save.png' alt="Rena width=520" height="344" wp-image-25391">