在给定文本中查找PHP中特定对象的模式

时间:2020-03-25 15:12:33

标签: php regex preg-match-all

我很难找到具有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/"]

现在我有这样的事情。那只是返回大量的URL。我只需要特定的

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

我的模式真的很糟糕,坚持了下来

1 个答案:

答案 0 :(得分: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