wordpress中preg_match正则提取和替换字符串

把以下代码:

 代码如下 复制代码

<img src="/wp-content/uploads/2014/02/hbzy1.gif" alt="hbzy"
 width="60" height="60" class="alignnone size-full wp-image-2100" />

输出显示的时候替换成:

 代码如下 复制代码

<div class="gif-box">
<img src="/wp-content/uploads/2014/02/hbzy1.jpg!static"
 _src="/wp-content/uploads/2014/02/hbzy1.jpg!static"
class="alignnone size-full wp-image-2100"
tsrc="/wp-content/uploads/2014/02/hbzy1.jpg" width="60" height="60" />
<div class="gif-loading-box">
<i class="gif-loading" style="display:none;"></i>
<i class="gif-play"></i>
</div>
</div>

这里是为了动态gif图片默认的时候不播放,点击才播放。这个时候需要用到preg_match和正则表达式匹配图片代码,并用str_replace进行字符串的替换和修改。

 代码如下 复制代码

function gif_content($content) {
 $content = get_the_content ();
 $img_html = preg_match ( '/(<img[^>]+>)/i', $content, $maches ); //获取img图片的html,如:<img src=".." class=".." width=""> //www.111cn.net
 $img_html = $maches [0];
 
 $img_width = preg_match ( '/width="[0-9]+"/i', $img_html, $maches ); //获取图片宽度,如:width="32"
 $img_width = str_replace('=', ':', $maches[0]);
 $img_width = str_replace('"', '', $img_width);
 $img_width = $img_width.'px'; //替换成width:32px
 
 $img_height = preg_match ( '/height="[0-9]+"/i', $img_html, $maches );
 $img_height = str_replace('=', ':', $maches[0]);
 $img_height = str_replace('"', '', $img_height);
 $img_height = $img_height.'px';
 
 $img_name = preg_match ( '!http://.+.(?:jpe?g|png|gif)!Ui', $img_html, $maches );
 $img_name = $maches [0];
 
 $img_html2 = str_replace ( 'src="' . $img_name . '"', 'src="' . $img_name . '!static" _src="' . $img_name . '!static' . '" tsrc="' . $img_name . '"', $img_html );
 $img_html2 = '<div class="gif-box" style="'.$img_width.';'.$img_height.'">' . $img_html2 . '<div class="gif-loading-box"><i class="gif-loading" style="display:none;"></i><i class="gif-play"></i></div></div>';//在图片的html代码前后加上div
 
 $content = str_replace ( $img_html, $img_html2, $content );
 return $content;
}
 
add_filter('the_content', 'gif_content', 10);//wordpress的钩子函数

这里是用在wordpress的文章形式中的,当文章形式是image的时候,给这个文章形式下的图片进行这样的操作。

另外在index.php中需要使用如下代码:

 代码如下 复制代码
$format=get_post_format();
if ('image'!=$format) {
 remove_filter('the_content', 'gif_content');
}
get_template_part( 'content', get_post_format() );

这里要判断一下如果是其他文章形式就移除这个钩子,否则所有文章形式的图片都被进行这样的操作。

本文讲的是preg_match正则提取和替换字符串,以及wordpress的文章形式。

时间: 2024-08-01 09:00:00

wordpress中preg_match正则提取和替换字符串的相关文章

PHP中preg_match正则匹配中的/u、/i、/s含义_php技巧

PHP中preg_match正则匹配的/u /i  /s是什么意思 /u 表示按unicode(utf-8)匹配(主要针对多字节比如汉字) /i 表示不区分大小写(如果表达式里面有 a, 那么 A 也是匹配对象) /s 表示将字符串视为单行来匹配

解析PHP正则提取或替换img标记属性

<?php/*PHP正则提取图片img标记中的任意属性*/$str = '<center><img src="https://img.lookmw.cn/images/20100516000.jpg" height="120" width="120"><br />PHP正则提取或更改图片img标记中的任意属性</center>'; //1.取整个图片代码preg_match('/<s*i

解析PHP正则提取或替换img标记属性_php技巧

<?php/*PHP正则提取图片img标记中的任意属性*/$str = '<center><img src="https://img.lookmw.cn/images/20100516000.jpg" height="120" width="120"><br />PHP正则提取或更改图片img标记中的任意属性</center>'; //1.取整个图片代码preg_match('/<\s*

又一不错的应用-正则来查找替换字符串_正则表达式

以下这段代码截取自某一网页源码: onClick="MM_openBrWindow('../photo/nlife/sfc/pages/O'Connors_jpg.htm','RD','width=456,height=368')" onClick="MM_openBrWindow('../photo/nlife/sfc/pages/Trad'r Sam_jpg.htm','RD','width=456,height=368')" onClick="MM_

又一不错的应用-正则来查找替换字符串

以下这段代码截取自某一网页源码: onClick="MM_openBrWindow('../photo/nlife/sfc/pages/O'Connors_jpg.htm','RD','width=456,height=368')" onClick="MM_openBrWindow('../photo/nlife/sfc/pages/Trad'r Sam_jpg.htm','RD','width=456,height=368')" onClick="MM_

PHP中preg_match函数正则匹配的字符串长度问题_php技巧

项目中,用preg_match正则提取目标内容,死活有问题,代码测得死去活来. 后来怀疑PHP 的preg_match有字符串长度限制,果然,发现"pcre.backtrack_limit "的值默认只设了100000. 解决办法:ini_set('pcre.backtrack_limit', 999999999); 注:这个参数在php 5.2.0版本之后可用. 另外说说关于:pcre.recursion_limit pcre.recursion_limit是PCRE的递归限制,这个

preg_match正则匹配的字符串

例 preg_match_all正则匹配字符串所有连接地址  代码如下 复制代码 $str ='<a href="http://www.a.com/2010/11-15/5.html">4</a>   <a href="http://www.b.com/2010/11-15/6.html">5</a>   <a href="http://www.b.com/2010/11-15/1.html"

preg_match正则匹配提示pcre.backtrack_limit解决办法

用preg_match正则提取目标内容,死活有问题,代码测得死去活来. 后来怀疑PHP 的preg_match有字符串长度限制,果然,发现"pcre.backtrack_limit "的值默认只设了100000. 解决办法:  代码如下 复制代码 ini_set('pcre.backtrack_limit', 999999999); 注:这个参数在php 5.2.0版本之后可用. 另外说说关于: pcre.recursion_limit pcre.recursion_limit是PCR

freemarker 替换字符串,模板,可以用来动态生成代码

freemarker 通过字符串模板生成,会把map中 name 对应的变量替换字符串模板中的${name} package com.thinkgem.jeesite.test.test; import java.io.IOException; import java.io.StringReader; import java.io.StringWriter; import java.util.HashMap; import java.util.Map; import freemarker.tem