php提取文章内容图片地址正则表达式

  1. <!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <meta http-equiv="content-type" content="text/html; charset=gb2312" />
  5. <title>php提取文章内容图片地址正则表达式</title>
  6. </head>
  7. <body>
  8. <form name="form1" method="post" action="">
  9. <p>
  10. <label for="body"></label>
  11. <textarea name="body" cols="45" rows="5"></textarea>
  12. </p>
  13. <p>
  14. <input type="submit" name="button" value="提交" />
  15. </p>
  16. </form>
  17. </body>
  18. </html>
  19. <?
  20. if( $_post )
  21. {
  22. $body = $_post['body'];
  23. preg_match_all("/(src|src)=["|'| ]{0,}((.*).(gif|jpg|jpeg|png|bmp))/isu",$body,$array);
  24. if( $array )
  25. {
  26. echo '取得图片地址';
  27. print_r($array);
  28. }
  29. else
  30. {
  31. echo '文章内容中不存在有效的图片地址';
  32. }
  33. }
  34. /*测试 ,输入
  35. 这里是www.phpfensi.com为你提供一款正则取处文章内容中的图片地址代码如下
  36. <img src=http://filesimg.phpfensi.com/2010/03/20100811004403298.gif />
  37. fdsafdsafsa
  38. 结果:
  39. 取得图片地址array
  40. (
  41. [0] => array
  42. (
  43. [0] => src=http://filesimg.phpfensi.com/2010/03/20100811004403298.gif
  44. )
  45. [1] => array
  46. (
  47. [0] => src
  48. )
  49. [2] => array
  50. (
  51. [0] => http://filesimg.111cn.net/2010/03/20100811004403298.gif
  52. )
  53. [3] => array
  54. (
  55. [0] => http://www.phpfensi.com/a
  56. )
  57. [4] => array
  58. (
  59. [0] => gif
  60. )
  61. )
  62. ?>