首页 > php代码 > php提取文章内容图片地址正则表达式

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

 代码如下 复制代码

<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=gb2312" />
<title>php教程提取文章内容图片地址正则表达式</title>
</head>

<body>
<form id="form1" name="form1" method="post" action="">
  <p>
    <label for="body"></label>
    <textarea name="body" id="body" cols="45" rows="5"></textarea>
  </p>
  <p>
    <input type="submit" name="button" id="button" value="提交" />
  </p>
</form>
</body>
</html>
<?
if( $_post )
{
 $body = $_post['body'];
 preg_match_all("/(src|src)=["|'| ]{0,}((.*).(gif|jpg|jpeg|png|bmp))/isu",$body,$array); 
 if( $array )
 {
  echo '取得图片地址';
  print_r($array);
 }
 else
 {
  echo '文章内容中不存在有效的图片地址';
 }
}

/*测试
输入

这里是www.phprm.com为你提供一款正则取处文章内容中的图片地址代码如下
<img src=http://filesimg.phprm.com/2010/03/20100811004403298.gif />
fdsafdsafsa
结果:
取得图片地址array
(
    [0] => array
        (
            [0] => src=http://filesimg.phprm.com/2010/03/20100811004403298.gif
        )

    [1] => array
        (
            [0] => src
        )

    [2] => array
        (
            [0] => http://filesimg.phprm.com/2010/03/20100811004403298.gif
        )

    [3] => array
        (
            [0] => http://www.phprm.com/a
        )

    [4] => array
        (
            [0] => gif
        )

)
?>



教程地址:http://www.phprm.com/code/34459.html

欢迎转载!但请带上文章地址^^

标签:none

发表留言