开发者

a crawler using php and Regex

i wanna a regex to capture any thing between <h1> and <br /> into matches['name']. and any thing开发者_StackOverflow中文版 between <h1 style="float: left;"> and </h2> to matches['cost']. how i should do it ?

Cheers,


This is not an HTML parser, it's just a regex based string search (Demo):

$searches = array(
    'name' => '<h1>(.*)<br />',
    'cost' => '<h1 style="float: left;">(.*)</h2>'
);

$matches = array();
foreach($searches as $name => $pattern)
{
    $r = preg_match_all("~{$pattern}~", $str, $matches[$name]);
    $matches[$name] = $matches[$name][1];
}

print_r($matches);

Output:

Array
(
    [name] => Array
        (
            [0] =>  name1
            [1] =>  name2
        )

    [cost] => Array
        (
            [0] =>  cost1
            [1] =>  cost1
        )

)


preg_match('/<h1 style="float: left;">(?P<cost>.*?)<\/h1>.*<h1>(?P<name>.*?)<br \/>/s', $string, $matches);

echo $matches['name'];
echo $matches['cost'];
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜