QQ咨询 官方微信

添加微信好友

官方小程序

百度小程序

微信小程序

头条小程序

关于我们

PHP基础

PHP经典实用正则表达式小结

 admin  2017-05-11 22:35:43
本文实例讲述了PHP经典实用正则表达式。分享给大家供大家参考,具体如下:

对于开发人员来说,正则表达式是一个非常有用的功能,它提供了 查找,匹配,替换 句子,单词,或者其他格式的字符串。这里介绍了几个超实用的php正则表达式,需要的朋友可以参考下。

1. 验证域名检验一个字符串是否是个有效域名
  1. <?php
  2. $url = "https://www.baidu.com";
  3. if (preg_match('/^(http|https|ftp):\/\/([A-Z0-9][A-Z0-9_-]*(?:.[A-Z0-9][A-Z0-9_-]*)+):?(d+)?\/?/i', $url)) { 
  4. echo "Your url is ok.";
  5. else 
  6. echo "Wrong url.";
2. 从一个字符串中 突出某个单词

这是一个非常有用的在一个字符串中匹配出某个单词 并且突出它,非常有效的搜索结果
  1. <?php$text = "Sample sentence from KomunitasWeb, regex has become popular in web programming. Now we learn regex. According to wikipedia, Regular expressions (abbreviated as regex orregexp, with plural forms regexes, regexps, or regexen) are written in a formal language that can be interpreted by a regular expression processor";
  2. $text = preg_replace("/(regex)/i"'<span style="background:#5fc9f6">1</span>', $text);
  3. echo $text; 
  1. function get_the_title(){  
  2. return 'Save the search.php file and open style.css. Append the following line to it: ';
  3. }
  4. $s = 'and php';
  5. $title  = get_the_title();
  6. $keys= explode(" ",$s);
  7. $title  = preg_replace('/('.implode('|', $keys) .')/iu','<strong>\0</strong>',$title);
  8. echo $title; 

3. 从HTML文档中获得全部图片

如果你曾经希望去获得某个网页上的全部图片,这段代码就是你需要的,你可以轻松的建立一个图片下载机器人
  1. <?php
  2. $images = array();
  3. $data = file_get_contents('https://www.baidu.com');
  4. preg_match_all('/(img|src)=("|\')[^"\'>]+/i', $data, $media);
  5. unset($data);
  6. $data=preg_replace('/(img|src)("|\'|="|=\')(.*)/i',"$3",$media[0]);
  7. foreach($data as $url)     { 
  8. $info = pathinfo($url); 
  9. if (isset($info['extension'])) {  
  10. if (($info['extension'] == 'jpg') ||  ($info['extension'] == 'jpeg') ||  ($info['extension'] == 'gif') ||  ($info['extension'] == 'png'))  array_push($images, $url); 
  11. }
  12. }
  13. var_dump($images); 
4. 匹配一个XML或者HTML标签

这个简单的函数有两个参数:第一个是你要匹配的标签,第二个是包含XML或HTML的变量,再强调下,这个真的很强大
  1. <?php
  2. function get_tag( $tag, $xml ) {  
  3. $tag = preg_quote($tag);  
  4. output($tag);  
  5. preg_match_all('/<'.$tag.'[^>]*>(.*?)<\/'.$tag.'>./',    $xml,    $matches,    PREG_PATTERN_ORDER  );  
  6. return $matches[1];}$xml = '<span>bb<a>bbb</a><a>ccc</a></span><span>bb<a>aa</a><p><a>ddd</a></p></span>';
  7. $tag = 'a';
  8. $return = get_tag($tag, $xml);
  9. var_dump($return);
  10. /*array(2) { 
  11.     [0]=> array(3) {  
  12.         [0]=>  string(11) "bbb<"  
  13.         [1]=>  string(10) "aa<"  
  14.         [2]=>  string(11) "ddd<" 
  15.     } 
  16.     [1]=> array(3) {  
  17.         [0]=>  string(3) "bbb"  
  18.         [1]=>  string(2) "aa"  
  19.         [2]=>  string(3) "ddd" 
  20.     }
  21. }
  22. array(3) { 
  23.         [0]=> string(3) "bbb" 
  24.         [1]=> string(2) "aa" 
  25.         [2]=> string(3) "ddd"
  26. }*/ 
PS:这里再为大家提供2款非常方便的正则表达式工具供大家参考使用:

JavaScript正则表达式在线测试工具:
http://tools.jb51.net/regex/javascript

正则表达式在线生成工具:
http://tools.jb51.net/regex/create_reg



¥ 打赏
×
如果您觉得文章帮助了您就打赏一下吧
非常感谢你的打赏,我们将继续分享更多优质内容,让我们一起创建更加美好的网络世界!

支付宝扫一扫打赏

微信扫一扫打赏

本文《PHP经典实用正则表达式小结》发布于石头博客文章,作者:admin,如若转载,请注明出处:https://www.pweb123.com/html/php/699.html,否则禁止转载,谢谢配合!

文章点评

我来说两句 已有0条评论
点击图片更换

添加微信好友

添加微信好友

微信小程序

百度小程序