DWQA QuestionsCategory: ProgramWhat's better about this kind of JSON string
PHP rookie asked 2 months ago
showdesc({"date":1505647332472,"content":"<div style=\"text-align: center;\"></div>"})

Like this kind of head and tail are included, how to extract the contents?
I used STR first_ Replace showdesc (and}) is not suitable

5 Answers
Su Sheng is not confused answered 2 months ago
$s='showdesc({"date":1505647332472,"content":"<div style=\"text-align: center;\"></div>"})';
preg_match('#showdesc\((.*)\)#',$s,$m);
//echo '<pre>';print_r($m);
print_r(json_decode($m[1],1));
TMaize answered 2 months ago

This is the data in jsonp format, that is, a JS code fragment. You can create a function with the same name and accept it

<script>
    function showdesc(data) {
        console.log(data.date);
        console.log(data.content);
    }
    showdesc({"date":1505647332472,"content":"<div style=\"text-align: center;\"></div>"});
    //var str = 'showdesc({"date":1505647332472,"content":"<div style=\"text-align: center;\"></div>"})';
    //eval(str);
</script>
Thorn bird answered 2 months ago

You need to see more regular

No son answered 2 months ago

If it is a fixed format, you can directly intercept the string.
Someone has already said the regular matching scheme.

krun answered 2 months ago

If it is fixed, then as said above, regular can be used
If it’s not fixed, you can find the first one from left to right
Then do a Java interception:

str.substring(str.indexOf("("), str.length - 1);

This string is called jsonp:
callback(json)
So find the intercept location