0

从短语:

 <div class="latestf"> <a href="http://www.x.ro/anamaria/"
 rel="nofollow"

我想提取anamaria。如何用 preg_match_all 做到这一点?

我试过了:

preg_match_all("'<div class=\"latestf\">
<a href=\"http://www.x.ro/(.*?)\" rel=\"nofollow\"'si", $source, $match);

但它没有工作......

先感谢您 !

4

3 回答 3

1

不要尝试使用正则表达式解析 HTML。改用DOM 解析器

$html = '<div class="latestf"> <a href="http://www.x.ro/anamaria/"
 rel="nofollow"';

$dom = new DOMDocument;
@$dom->loadHTML($html);
foreach ($dom->getElementsByTagName('a') as $node)
{
    $link = $node->getAttribute("href");
}

$parsed = parse_url($link);

echo substr($parsed['path'], 1, -1);

输出:

anamaria

演示!

于 2013-09-09T12:25:14.507 回答
1

尝试这个:

$source = '<div class="latestf"> <a href="http://www.x.ro/anamaria/" rel="nofollow"';


preg_match_all('#<div\s*class="latestf">\s*<a\s*href="http://www\.x\.ro/(.*?)/?"\s*rel="nofollow"#i', $source, $match);

print_r($match);

Array
(
    [0] => Array
        (
            [0] => <div class="latestf"> <a href="http://www.x.ro/anamaria/" rel="nofollow"
        )

    [1] => Array
        (
            [0] => anamaria
        )

)
于 2013-09-09T12:22:01.693 回答
0

/应该像这样逃脱\/

<?php

  $source = '<div class="latestf"> <a href="http://www.x.ro/anamaria/" rel="nofollow"';

  preg_match_all('/<div class="latestf"> <a href="http:\/\/www.x.ro\/(.*?)\/" rel="nofollow"/', $source, $match);

  var_dump($match);exit;
于 2013-09-09T12:43:37.260 回答