-1

I have trouble to find specific object with preg_match_all pattern. I have a text. But I would like to find just one specific

Like I have a string of text

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 */

However I just need to find "website":["https://bitcoin.org/"]. Where website is dynamic data. Such as website can be a google "website":["https://google.com/"]

Right now I have something like this. That's just return a bulk of urls. I need just specific

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

I am really bad in patterns and stuck on that

Arthur Yakovlev
  • 8,933
  • 8
  • 32
  • 48

1 Answers1

1

You can get all the data that follows the website prefix until the next " comes [^"]+:

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

The matches[1] is to get the first match (what matches the content inside the parentheses).

This prints:

https://bitcoin.org/

as you can check here

jeprubio
  • 17,312
  • 5
  • 45
  • 56