簡體   English   中英

preg_match_all在兩個句子之間

[英]preg_match_all between two sentences

從短語:

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

但它沒有用......

先感謝您 !

嘗試這個:

$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
        )

)

不要嘗試使用正則表達式解析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

演示!

/應該像這樣轉義\\/

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM