php url 正则表达式用法

下面二款php 正则来表达式来处理url匹配的代码,希望对你有点帮助.

  1. <?php
  2. $url = 'http://www.phpfensi.com/zongzi/oo.html';
  3. $n = preg_match_all("/http:[/]{2}[a-z]+[.]{1}[a-zd-]+[.]{1}[a-zd]*[/]*[A-Za-zd]*[/]*[A-Za-zd]*[.]*html/",$url,$array);
  4. var_dump($array);
  5. ?>

例子:

  1. $url = 'http://www.test.com/icons/favicon-54.ico, http://www.baidu.com/bb, http://www.phpfensi.com, http://img.baidu.com/logo.ico';
  2. $pattern = "|http://[^,]+?.ico,?|U";
  3. preg_match_all($pattern, $url, $matches);
  4. print_r($matches);
  5. //输出结果:
  6. Array
  7. (
  8. [0] => Array
  9. (
  10. [0] => http://www.test.com/icons/favicon-54.ico
  11. [1] => http://img.baidu.com/logo.ico
  12. )
  13. )