Home  >  Article  >  Backend Development  >  wordpress article image attribute replacement

wordpress article image attribute replacement

WBOY
WBOYOriginal
2016-12-01 00:56:211175browse

最近用wordpress建了个小图片站,有点问题请教下:

文章页代码:








问题1,图片路径中有空格,如何使用php把空格全部替换成杠"-",由1112 cat 变成 1112-cat
问题2,替换空格后,在图片路径中.jpg后面加上七牛图片样式 !width300

请教这两个问题如何用php解决

回复内容:

最近用wordpress建了个小图片站,有点问题请教下:

文章页代码:








问题1,图片路径中有空格,如何使用php把空格全部替换成杠"-",由1112 cat 变成 1112-cat
问题2,替换空格后,在图片路径中.jpg后面加上七牛图片样式 !width300

请教这两个问题如何用php解决

图片站的图片是本地上传还是抓取的图片?最后图片是上传至七牛的?上传或者抓取后,正则按照你说明的替换,后面追加上你提到的参数。

不知道是不是我没有理解到你的难点在哪?可以继续回复追问,后续我电脑给你更新回复。

  1. 问题1

提醒一句,就算你用php将空格替换了,但是如果你服务器上的目录名称没有修改,你的图片打开只能是404。
php的函数,你可以用str_replace,这个函数在我的网站历史文章中有过应用,你可以在文中搜索str_replace,查看第二个结果。

  1. 问题2

可以参考这个:

<code>/**
 * 七牛缩略图和水印
**/
function QiNiuThumbnailWatermark($content) {
   global $post;
   $pattern ="/<img(.*?)src=('|\")(.*?).(bmp|gif|jpeg|jpg|png)('|\")(.*?)>/i";
   /* 下面这行代码中的dengxiang是七牛样式中的样式名称,而斜杠为七牛中设置的样式分割符 */
   $replacement = '<img$1src=$2$3.$4/dengxiang$5$6>';
   $content = preg_replace($pattern, $replacement, $content);
   return $content;
}
add_filter('the_content', 'QiNiuThumbnailWatermark');
?></code>

具体使用方法可以看这里:七牛图片处理样式的正确使用方式

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn