php正则表达式之preg_match()用法

利用 preg_match(),我们可以完成字符串的规则匹配。如果找到一个匹配,preg_match() 函数返回 1,否则返回 0。还有一个可选的第三参数可以让你把匹配的部分存在一个数组中。在验证数据时这个功能可以变得非常有用。

实例代码如下:

  1. <?php 
  2. // 模式定界符后面的 "i" 表示不区分大小写字母的搜索 
  3. if (preg_match ("/php/i""PHP is the web scripting language of choice.")) { 
  4. print "A match was found."
  5. else { 
  6. print "A match was not found."
  7. ?> 

取得当前时间

实例代码如下:

  1. <?php 
  2. //需要匹配的字符串。date函数返回当前时间。 "现在时刻:2012-04-20 07:31 am" 
  3. $content = "现在时刻:".date("Y-m-d h:i a"); 
  4. //匹配日期和时间. 
  5. if (preg_match ("/d{4}-d{2}-d{2} d{2}:d{2} [ap]m/"$content$m)) 
  6. echo "匹配的时间是:" .$m[0]. "n"//"2012-04-20 07:31 am" 
  7. //分别取得日期和时间 
  8. if (preg_match ("/([d-]{10}) ([d:]{5} [ap]m)/"$content$m)) 
  9. echo "当前日期是:" .$m[1]. "n"//"2012-04-20" 
  10. echo "当前时间是:" .$m[2]. "n"//"07:31 am" 
  11. ?> 

这个例子将验证出此 Email 地址为正确格式。现在让我们来看看这段正则表达式所代表的各种规则。

获取Google首页title

比如说要获取google首页的title内容,代码如下:

实例代码如下:

  1. <?php 
  2. $str = file_get_contents('http://www.google.com'); 
  3. preg_match('/<title>(.*)</title>/'$str$arr); 
  4. echo $arr[1]; 
  5. ?> 

从网址获取域名

实例代码如下:

  1. <?php 
  2. preg_match("/^(http://)?([^/]+)/i""http://www.111cn.net/index.html"$matches); 
  3. $host = $matches[2]; // 从主机名中取得后面两段 
  4. preg_match("/[^./]+.[^./]+$/"$host$matches); 
  5. echo "domain name is: {$matches[0]}n"
  6. ?> 

preg_match($pattern,$string,$matcher)其中$pattern对应的就是/^(http://)?([^/]+)/i,$string 是http://www.php.net/index.html,$match是匹配到的结果。

如果提供了 matches,则其会被搜索的结果所填充。$matches[0] 将包含与整个模式匹配的文本,$matches[1] 将包

含与第一个捕获的括号中的子模式所匹配的文本,以此类推。

$matches[0] 将包含与整个模式匹配的文本。咱们用pring_r打印出来第一个$matches:

实例代码如下:

  1. Array (  
  2.  [0] => http://www.111cn.net  
  3.  [1] => http://  
  4.  [2] => http://www.phpfensi.com ) 

$matches[0] 将包含与整个模式匹配的文本,$matches[1] 将包含与第一个捕获的括号中的子模式所匹配的文本。在正则中,()代表模式:匹配 pattern 并获取这一匹配。所获取的匹配可以从产生的 Matches 集合得到,在VBScript 中使用 SubMatches 集合,在JScript 中则使用 $0…$9 属性。就是说数组中下标为1的值就是正则中/^(http://)?([^/]+)/i第一个()里的值!数组下标2的值以此类推。

波比源码 – 精品源码模版分享 | www.bobi11.com
1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长!
2. 分享目的仅供大家学习和交流,您必须在下载后24小时内删除!
3. 不得使用于非法商业用途,不得违反国家法律。否则后果自负!
4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
5. 如有链接无法下载、失效或广告,请联系管理员处理!
6. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需!
7. 本站源码并不保证全部能正常使用,仅供有技术基础的人学习研究,请谨慎下载
8. 如遇到加密压缩包,请使用WINRAR解压,如遇到无法解压的请联系管理员!

波比源码 » php正则表达式之preg_match()用法

147 评论

  1. order sildenafil 100mg online cheap bimatoprost ca methocarbamol 500mg for sale

  2. purchase tadalis generic generic avanafil buy diclofenac without prescription

  3. indomethacin order online lamisil usa purchase amoxicillin online cheap

  4. purchase ramipril online irbesartan oral astelin 10ml drug

  5. order omeprazole 20mg pills pay for essays online blackjack with real money

  6. dapoxetine 90mg brand priligy price order synthroid 100mcg sale

  7. brand tamsulosin 0.2mg oral zofran spironolactone 100mg canada

  8. order prednisone 20mg generic vermox 100mg usa buy mebendazole for sale

  9. order spiriva 9 mcg without prescription minocycline brand purchase terazosin without prescription

  10. montelukast 10mg cost viagra next day buy sildenafil 100mg online

  11. order cialis 10mg generic Buy cialis empire city casino online

  12. purchase tadacip online cheap tadacip cost amoxicillin 250mg

  13. buy esomeprazole 20mg generic biaxin drug lasix 100mg price

  14. sildenafil 50mg canada finasteride oral order finasteride 1mg generic

  15. prednisolone 10mg canada oral lasix order lasix without prescription

  16. order albendazole 400 mg online albenza cheap medroxyprogesterone 5mg canada

  17. Thank you for great content. Hello Administ. Hacklink Panel Hacklink

  18. buy carbamazepine 400mg without prescription oral tegretol order lincocin

  19. generic cialis 5mg cialis 20mg cheap cheap sildenafil without prescription

  20. cleocin pills erythromycin oral fda approved over the counter ed pills

  21. buy generic temovate for sale cheap clobetasol buy amiodarone generic

  22. finasteride price proscar 5mg uk buy viagra 100mg pill

  23. buy avlosulfon 100 mg for sale oral dapsone buy cheap generic aceon

  24. order arcoxia 120mg without prescription order arcoxia sale astelin 10 ml uk

  25. tacrolimus cheap remeron brand ropinirole 1mg for sale

  26. buy oxcarbazepine generic brand alfuzosin order generic ursodiol 300mg

  27. fluoxetine 40mg cost generic revia brand letrozole

  28. how to buy bisoprolol zebeta 5mg drug order oxytetracycline

评论已关闭

Hi, 如果你对这款模板有疑问,可以跟我联系哦!

联系站长
赞助VIP 享更多特权,建议使用 QQ 登录
喜欢我嘛?喜欢就按“ctrl+D”收藏我吧!♡