I
I
IliaBrz2019-01-31 18:12:48
PHP
IliaBrz, 2019-01-31 18:12:48

How can you parse such a string?

Good evening.
How can you parse such a string using PHP? I'm interested in getting the Result parameter from a tag.

<html><content></content><json><![CDATA[{"type":"datasource", "Result":true,"HasVKBinding":true}]]><![CDATA[{"type":"webparams", "ClientScriptId":"string-CommonController","ContainerId":"string-CommonController","ClientControlClass":"string-CommonController","DisableDomRendering":"bool-False","ExpireMinutes":"integer-0"}]]></json></html>

Thanks for your help
Ilya

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Soslan Khloev, 2019-01-31
@hloe0xff

You can use simplexml_load_string . The output will be an object that is easier to work with.

$xml = simplexml_load_string('<html><content></content><json><![CDATA[{"type":"datasource", "Result":true,"HasVKBinding":true}]]><![CDATA[{"type":"webparams", "ClientScriptId":"string-CommonController","ContainerId":"string-CommonController","ClientControlClass":"string-CommonController","DisableDomRendering":"bool-False","ExpireMinutes":"integer-0"}]]></json></html>');
$array_from_json = json_decode($xml->json, 1);

D
DanKud, 2019-01-31
@DanKud

If the string construction is always the same, then you can do this:

$html = 'ваша_строка';
preg_match('/"Result":(.*?),/is', $html, $m);
$result = $m[1];
var_dump($result); /* string(4) "true" */

If JSON can change, then you can decode the JSON itself like this:
$html = 'ваша_строка';
preg_match('/CDATA\[(.*?)\]/is', $html, $m);
$json = $json = json_decode($m[1], true);
$result = $json['Result'];
var_dump($result); /* bool(true) */

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question