警告:preg_match_all()[function.preg-match-all]:未知修饰符'g'

0 php preg-match-all

错误:

警告:preg_match_all()[function.preg-match-all]:第23行/Users/julian/Sites/abc.php中的未知修饰符'g'警告:preg_match_all()[function.preg-match-all]:未知第23行/Users/julian/Sites/abc.php中的修饰符'g'

这是我的代码:

<?php

class Crawler {
protected $markup = ”;
    public function __construct($uri) {
        $this->markup = $this->getMarkup($uri);
    }
    public function getMarkup($uri) {
        return file_get_contents($uri);
    }
    public function get($type) {
        $method = "_get_links";
        if (method_exists($this, $method))
                return call_user_method($method, $this);
             }
    }
    protected function _get_images() {
        if (!empty($this->markup)){
            preg_match_all(htmlspecialchars("<img([^>]+)/>i"), $this->markup, $images);
            return $images[1];
    }
    }
    protected function _get_links() {
        if (!empty($this->markup)){
            preg_match_all(htmlspecialchars("<a([^>]+)>(.*?)</a>/i"), $this->markup, $links);
            return $links;
        }
    }
}
$crawl = new Crawler("http://google.com/");
$images = $crawl->get(‘images’);
$links = $crawl->get(‘links’);
echo $links;
?>
Run Code Online (Sandbox Code Playgroud)

Nik*_*kiC 5

你缺乏分隔符.正确的正则表达式是:

?             ?
~<img([^>]+)/>~i
~<a([^>]+)>(.*?)</a>~i
?                   ?
Run Code Online (Sandbox Code Playgroud)

但请注意,通常不建议使用正则表达式解析HTML.相反,您可以考虑使用DOM.

注意:PHP中的未知修饰符'g'没有你使用的g修饰符,preg_match_all()而不是preg_match()你想要所有的匹配.