从网站php中提取信息无法正常工作

        <?php
        $url = "http://www.lolking.net/summoner/euw/20849404";
        $str = file_get_contents($url);
        if (preg_match('/<ul class="personal_ratings"> ([^<]*)<\/ul>/', $str, $matches) > 0) {
            echo $matches[1]; //This is text one
        }

        ?>

Hello guys, I'm trying to extract all the personal ratings info from $url I provided, bur for some reason I can't find a way to extract it like this, I've been trying a ton of different ways but I guess it's my lack of regex knowlede working here, it's probably a small dumb mistake but I've been looking to fix this for the last few days now.

I see few mistakes:

  1. Parts of your expression that you consider as plain text are containing regex special characters. You may consider escaping them or using preg_quote() function or using ~ instead of / to wrap expression.
  2. If <ul> node is split to more than one line of html code, you need to add s switch.
  3. I would personally use (.*) with ungreedy switch (U at the end of expression)

I don't have time to check this, but this one should work if site structure is same as in your expression: preg_match('~<ul class="personal_ratings">(.*)</ul>~Us', ...

Anyway, for parsing HTML it is much easier to use some DOM parser, like PHP Simple HTML DOM Parser. It is as easy as:

$html = file_get_html('http://www.lolking.net/summoner/euw/20849404');
$ratings = $html->find('ul.personal_ratings',0)->innertext;

PS. Regex special characters are: . \ + * ? [ ^ ] $ ( ) { } = ! < > | : -