简体   繁体   English

preg_match_all在两个句子之间

[英]preg_match_all between two sentences

from the phrase: 从短语:

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

I want to extract anamaria. 我想提取anamaria。 How to do that with preg_match_all ? 如何用preg_match_all做到这一点?

I tried: 我试过了:

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

but it didn`t work... 但它没有用......

Thank you in advance ! 先感谢您 !

Try this: 尝试这个:

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

)

Don't try to parse HTML with regex. 不要尝试使用正则表达式解析HTML。 Use a DOM parser instead: 改为使用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);

Output: 输出:

anamaria

Demo! 演示!

/ should be escaped like this \\/ /应该像这样转义\\/

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