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

使用智能引号代替双引号
如果你是一个印刷爱好者,你将喜欢这个允许用智能引号代替双引号的正则表达式,这个正则被WORDPRESS在其内容上使用

preg_replace('B"b([^"x84x93x94rn]+)b"B', '?1?', $text);

检验密码的复杂度
这个正则表达式将检测输入的内容是否包含6个或更多字母,数字,下划线和连字符. 输入必须包含至少一个大写字母,一个小写字母和一个数字

复制代码 代码如下:

'A(?=[-_a-zA-Z0-9]*?[A-Z])(?=[-_a-zA-Z0-9]*?[a-z])(?=[-_a-zA-Z0-9]*?[0-9])[-_a-zA-Z0-9]{6,}z'  

 

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="https://www.jb51.net/smileys/souriant.png">',$texte);

移除图片的链接

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <?php $str = ' <a href="https://www.5idev.com/">5idev</a>其他字符 <a href="https://www.sohu.com/">sohu</a> <a href="https://www.sohu.com/"><img src="https://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正则表达式,希望对大家的学习有所帮助。

您可能感兴趣的文章:

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wjpdsg.html