【问题标题】:Pattern to find specific object in PHP in given text [duplicate]在给定文本中查找特定对象的模式 [重复]
【发布时间】:2020-03-25 15:12:33
【问题描述】:

我很难找到具有preg_match_all 模式的特定对象。我有一个文本。但我只想找到一个特定的

就像我有一串文本

sadasdasd:{"website":["https://bitcoin.org/"]tatic/cloud/img/coinmarketcap_grey_1.svg?_=60ffd80');display:inline-block;background-position:center;background-repeat:no-repeat;background-size:contain;width:239px;height:41px;} .cqVqre.cmc-logo--size-large{width:263px;height:45px;}
/* sc-component-id: sc-2wt0ni-0 */

但是我只需要找到"website":["https://bitcoin.org/"]。网站是动态数据。比如网站可以是google"website":["https://google.com/"]

现在我有这样的东西。那只是返回大量的网址。我只需要具体的

$pattern = '#\bhttps?://[^,\s()<>]+(?:\([\w\d]+\)|([^,[:punct:]\s]|/))#';
preg_match_all($pattern, $parsePage, $matches);
print_r($matches[0]);

我的模式真的很糟糕,一直坚持下去

【问题讨论】:

    标签: php regex preg-match-all


    【解决方案1】:

    在下一个"[^"]+之前,你可以获取网站前缀后面的所有数据:

    $parsePage = <<<PAGE
    sadasdasd:{"website":["https://bitcoin.org/"]tatic/cloud/img/coinmarketcap_grey_1.svg?_=60ffd80');display:inline-block;background-position:center;background-repeat:no-repeat;background-size:contain;width:239px;height:41px;} .cqVqre.cmc-logo--size-large{width:263px;height:45px;}
    /* sc-component-id: sc-2wt0ni-0 */';
    PAGE;
    
    $pattern = '#"website":\["(https?://[^"]+)#';
    preg_match($pattern, $parsePage, $matches);
    print_r($matches[1]);
    

    matches[1] 是获取第一个匹配项(匹配括号内的内容)。

    打印出来:

    https://bitcoin.org/
    

    您可以查看here

    【讨论】:

    • 但是可以有大量的网址。我只需要来自“网站”的网址:[“”]
    • 好的,然后检查更新的代码
    • 谢谢。爱你
    猜你喜欢
    • 1970-01-01
    • 2020-09-22
    • 2017-10-03
    • 1970-01-01
    • 1970-01-01
    • 2016-04-13
    • 2019-02-26
    • 2021-11-30
    • 2020-05-20
    相关资源
    最近更新 更多