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文章底部去除与添加版权信息的方法

wordpress禁止特定用户修改密码的方法

菜鸟使用wordpress建站的几点心得

wordpress中用于获取搜索表单的php函数使用解析

crayon syntax highlighter代码高亮插件与fancybox图片暗箱冲突的解决方法

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

wordpress上传图片不自动生成缩略图的解决方法

wordpress暴路径 影响所有版本

wordpress 的主要特点

如何解决wordpress更改新域名后使得网站正常运行

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



打赏

取消

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

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

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

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

评论

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