Предупреждение: preg_match_all () [function.preg-match-all]: неизвестный модификатор 'g' - PullRequest
0 голосов
/ 19 января 2011

Ошибки:

Предупреждение: preg_match_all () [function.preg-match-all]: неизвестный модификатор 'g' в /Users/julian/Sites/abc.php в строке 23 Предупреждение:preg_match_all () [function.preg-match-all]: неизвестный модификатор 'g' в /Users/julian/Sites/abc.php в строке 23

Вот мой код:

<?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;
?>

Ответы [ 2 ]

5 голосов
/ 19 января 2011

Вам не хватает разделителей.Правильные регулярные выражения:

↓             ↓
~<img([^>]+)/>~i
~<a([^>]+)>(.*?)</a>~i
↑                   ↑

Но учтите, что обычно не рекомендуется разбирать HTML с помощью регулярных выражений.Вместо этого вы можете рассмотреть возможность использования DOM .

Примечание: Неизвестный модификатор 'g' в PHP отсутствует g модификатор , вы просто используете preg_match_all() вместо preg_match(), если вы хотите все совпадения.

1 голос
/ 19 января 2011

Попробуйте это:

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("/<img([^>]+)\/>/i", $this->markup, $images);
        return $images[1];
}
}
protected function _get_links() {
    if (!empty($this->markup)){
        preg_match_all("/<a([^>]+)\>(.*?)\<\/a\>/i", $this->markup, $links);
        return $links;
    }
}
}
$crawl = new Crawler("http://google.com/");
$images = $crawl->get(‘images’);
$links = $crawl->get(‘links’);
print_r($links);
...