15个超实用的php正则表达式


当前第2页 返回上一页

WordPress: 使用正则获得 帖子上的图片
我知道很多人是WORDPRESS的使用者,你可能会喜欢并且愿意使用 那些从帖子的内容检索下来的图像代码。使用这个代码在你的BLOG只需要复制下面代码到你的某个文件里

<?php if (have_posts()) : ?> 
<?php while (have_posts()) : the_post(); ?> 
 
<?php 
$szPostContent = $post->post_content; 
$szSearchPattern = '~<img [^>]* />~'; 
 
// Run preg_match_all to grab all the images and save the results in $aPics 
preg_match_all( $szSearchPattern, $szPostContent, $aPics ); 
 
// Check to see if we have at least 1 image 
$iNumberOfPics = count($aPics[0]); 
 
if ( $iNumberOfPics > 0 ) { 
   // Now here you would do whatever you need to do with the images 
   // For this example the images are just displayed 
   for ( $i=0; $i < $iNumberOfPics ; $i++ ) { 
     echo $aPics[0][$i]; 
   }; 
}; 
 
endwhile; 
endif; 
?> 

自动生成笑脸图案
被WordPress使用的另一个方法, 这段代码可使你把图像自动更换一个笑脸符号

$texte='A text with a smiley '; 
echo str_replace(':-)','<img src="smileys/souriant.png">',$texte); 

移除图片的链接

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<?php 
  $str = ' 
    <a >5idev</a>其他字符 
    <a >sohu</a> 
    <a ><img src="http://www.fashion-press.net/img/news/3176/mot_06.jpg" /></a> 
    <br>'; 
 
 
  //echo preg_replace("/(<a.*?>)(<img.*?>)(<\/a>)/", '$2', $str);  
  echo preg_replace("/(<a.*?>)(<img.*?>)(<\/a>)/", '\2', $str);  
?> 

以上就是15个超实用的php正则表达式,希望对大家的学习有所帮助。


标签:WordPress

返回前面的内容

相关阅读 >>

wordpress(wp)3.5版修改默认上传图片路径的解决方法

wordpress 上传附件中文文件名乱码解决办法(for windows)

百度官方推出的wordpress结构化数据插件百度sitemap

wordpress分类,标签,分页显示及翻页出现404错误的解决方法

wordpress分类描述支持显示图片的功能实现方法

wordpress获取某个栏目链接地址的方法

wordpress搭建中英文双语言或多语言的网站详解

wordpress评论中禁止html代码显示的方法

wordpress入门指南-wordpress安装使用说明

wordpress添加并整合google自定义搜索实现步骤及代码

更多相关阅读请进入《wordpress》频道 >>



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...