-1

I have trouble to find specific object with preg_match_all pattern. I have a text. A lot datas. 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
  • 7
    why would you use a regex to deal with JSON? Why not parse the JSON to an object and then find the relevant information by accessing the correct property? That's the normal way. – ADyson Mar 25 '20 at 13:51
  • There are a really hude data. I just need this one. It's easy to get the string with file_get_contents. json_encode will 'eat' a lot of server memory. Most easy to parse the string. However I am bad in patterns – Arthur Yakovlev Mar 25 '20 at 13:56
  • how huge are we talking, exactly? It's text. How big is the file, in reality? Have you directly experienced a memory issue when trying to parse it? have you tested how much memory is really used? – ADyson Mar 25 '20 at 13:57
  • I doubt that it will eat server memory. – u_mulder Mar 25 '20 at 13:57
  • That's not just a json. A lot trash like random text as well. Some kbs of the texts – Arthur Yakovlev Mar 25 '20 at 13:59
  • What is your end goal here? Matching the `website` value and returning what exactly? Top level object with that property? – El_Vanja Mar 25 '20 at 14:00
  • You can use a regex to extract JSON from HTML of course. Entirely unanswerable without context/sample however. – mario Mar 25 '20 at 14:01
  • 1
    "Some kbs"... a few kilobytes will not harm your server. "not just a json"...why would a server provide data in multiple different formats simultaneously? That's very unlikely and doesn't make a lot of sense. Are you really sure? Is it actually a HTML document and you're trying to get Javascript code from it, or something? We have to see a proper example. – ADyson Mar 25 '20 at 14:01
  • With a couple of little tweaks to make what you posted into valid JSON (I assume you just left out the full string to reduce the sample), then it's trivial. Assuming $data contains the JSON: `$obj = json_decode($data, true); echo $obj["info"]["data"][1]["urls"]["website"][0];` Demo: http://sandbox.onlinephpfunctions.com/code/7dc04b313b00e4db755e184b6352e3a8ea1d4407 – ADyson Mar 25 '20 at 14:04
  • 1
    This really smells like an XY problem. Can you give us more context about the data you're working with and what you're actually trying to do? – El_Vanja Mar 25 '20 at 14:06
  • That's full sourse page which I am getting by file_get_contents("some site"), there are I just need to find specific string "website":["https://google.com/"], which can be any link "website":["$some_link"] – Arthur Yakovlev Mar 25 '20 at 14:08
  • Ok so really you are trying to parse a HTML document, not JSON data, is that correct? It helps to be clear about the full situation when you ask your questions. – ADyson Mar 25 '20 at 14:09
  • Yes, your right. But in question I did not said nothing about parsing JSON. I know how to do that. As I asked "I have to find some specific JSON object in given text". Thanks bro – Arthur Yakovlev Mar 25 '20 at 14:15
  • "I did not said nothing about parsing JSON"...the title and the wording of the question specifically mention JSON. As it is, if you're parsing a HTML document there's unlikely to be any JSON there. What you're probably looking at is a Javascript object literal. JSON is a text format, not a valid piece of code. (It so happens the syntax for JS literals and JSON does overlap, but the specifications are not identical.) – ADyson Mar 25 '20 at 14:15
  • Okay I need to find specific data in text "website":["$some_link"] – Arthur Yakovlev Mar 25 '20 at 14:18
  • So while you probably could decode that code as if it were JSON (from PHP's point of view) if you can locate it in the HTML, and it contains purely literal declarations without any JS function calls or similar, it technically _isn't_ JSON, and in your question, describing it as JSON, and posting _only_ that data, without explaining the context, led people, quite understandably, to think that you had _only_ that data, and wanted to get information from it. Hence the suggestion to just parse it. We can't read your screen, your disk...or your mind. You have to make the whole scenario 100% clear. – ADyson Mar 25 '20 at 14:19
  • Anyway I can't answer while the question is closed, but if I were you'd I'd write a regex which searches for the literal text `"website":[`, then allows any number of any characters in between, and then ends by looking for the literal text `]`. I'd wrap the "any characters" bit in a group, so you can extract just that bit once you've got the result. If you find a regex tutorial / reference site, you should be able to figure out how to allow "any number of any characters", and how to do grouping. – ADyson Mar 25 '20 at 14:21
  • P.S. to stand any chance of the question being re-opened you'll really need to edit it, to add all the context you've just given in the comments. – ADyson Mar 25 '20 at 14:22
  • @ADyson check out last my question please. Thanks – Arthur Yakovlev Mar 25 '20 at 15:24
  • 1
    How exactly does the third dupe fail to answer this? And what's the holdup with turning `"website":["………"]` into a pattern? (Other than tutorial aversion.) – mario Mar 25 '20 at 15:25

1 Answers1

0

Loop through the array like this:

    for ($x = 0; $x <= count(json_decode($array, true)) - 1; $x++) {
        if ($array[$x] == "your search criteria") {
            echo $array[$x];
            break;
        }
    }
Mech
  • 3,952
  • 2
  • 14
  • 25