使用preg_match_all函数提取图像

时间:2013-04-02 07:56:45

标签: php image

我有一个preg_match_all的问题,这是我的PHPfile中的一个函数:

function get_images ($content){

  preg_match_all ('#\\[img\\](.+?)\\[/img\\]#ie', $content, $preg_array);
  if (count ($preg_array[1]) != 0){
    foreach ($preg_array[1] as $item){
        if ($this->reset_url($_SERVER['HTTP_HOST'])!=$this->reset_url($item)){
            if (!(in_array ($item, $this->images))){
                $this->images[] = $item;
                continue;
            }
        }
    }
  }
}

使用该代码,我只能从简单的[img] [/ img]标签中提取图像,例如:

[img]http://www.domain.com/image.jpg[/img]

我不能使用带有对齐的img标签,例如:

[img=left]http://www.domain.com/image.jpg[/img]

我如何修复此功能以使用两个图像标签?

2 个答案:

答案 0 :(得分:3)

#\[img(:?.*)?\](.*?)\[/img\]#

这会忽略[img=foo]中的任何内容,只会获取源信息。

答案 1 :(得分:2)

使用此,

preg_match_all ('#\\[img.*\\](.+?)\\[/img\\]#ie', $content, $preg_array);

Codeviper Demo.