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
相关阅读 >>
crayon syntax highlighter代码高亮插件与fancybox图片暗箱冲突的解决方法
wordpress(wp)3.5版修改默认上传图片路径的解决方法
更多相关阅读请进入《wordpress》频道 >>