Hello. I would be very grateful for the help. There is a site in the html page of this site is:

<script id='PageJson' type='text/json'> { "age":"32", "name":"David", "surname":"Johnson" } </script> 

and I want to get it age, name, surname , and paste it to my site this way:

the person inserts the site link into the input , presses the button , and at the bottom, under the input, for example, the following appears:

 <center><b>David Johnson</b> имеет возраст <b>32</b> года</center> 

Please tell me in what ways this can be done?

  • You can parse it on the server, and then return it as JSON. - jashka
  • I understand --- rob --- parse this code You will not be from your site, but from some VKontakte? Find out if they have an API, this will be the most correct solution. And if your site, then why parse them, if you already have this data on your server ??? - Makarenko_I_V
  • and you will not prompt the code? for example, from the site coub there is <script id = 'PageJson' type = 'text / json'> {"title": "example", "audio": "example.mp3", "video": "example.mp4"} </ script> - vsemeruk

2 answers 2

Download HTML parser PHP Simple HTML DOM Parser .
Further as in a manna to the parser: parse, search for an object, take the contents, parse through json_decode .

 $dom = file_get_html('http://path.to/your/page'); // Парсим с URL, можно со строки: str_get_html $content = $dom->find('#PageJson', 0); // Ищем по ID Ваши данные if($content !== null) // Если найдено $content = json_decode($content->innertext); // Декодируем контент 

    I'm certainly not a master of parsing sites, but here’s an option:

     <?php $content = file_get_contents('https://goo.gl/P7lZil'); $pattern = '=\<script id\=\'PageJson\'.*?\>(.*)\</script\>=s'; $replacement = '$1'; echo preg_replace( $pattern, $replacement, $content ); 

    The result will be JSON, which, I think, you know how to manage ...

    Just in case here: json_decode .

    • Chur you, my friend. HTML can not parse regular! - user207618 pm
    • @Other, I know: heard; and why it is impossible - do not share a short clear article about it? What I remember is that it’s just that HTML is changeable - well, everything changes around. - Roman Grinyov 5:26 pm
    • I don’t know any articles (although there is definitely something on the Habré), I’m just sure that parsing the PL is a perversion, because you need to take into account possible constructions, notation, and it’s banal what quotation marks are in a string (what if it’s not a string, but a number?). If you really want, then you can, of course, no one forbids, but this is a sure way to get confused and spend N of the search time: Which of the ten brackets in the regular schedule is responsible for the ability to capture the third attribute? - user207618
    • Although not an article, but briefly and clearly: stackoverflow.com/a/1732454/1510966 Well, next to it there are more scientific explanations, using terms like "Chomsky's grammar" .. - Alexey Ukolov