简体   繁体   中英

How to check if string is a javascript object or not in php?

I have a string here:

{eventStart: "2018/12/01",eventEnd: "2018/12/01"}

I want to check if this can make a valid js object or not. Like if want to make an ajax call we write like this :

$.ajax({
    type: "post",
    url: "something",
    data: data
})

if we miss a comma or inverted comma there is a syntax error. I want to check it in php if the string can make a valid js object or not.

Above example, will check only for

{
    type: "post",
    url: "something",
    data: data
}

EDIT

I can check for valid json but for valid json in json_decode function, it should be like this :

{
    "type": "post",
    "url": "something",
    "data": data(valid object here)
}

but when we write code we do just this

{
    type: "post",
    url: "something",
    data: data
}

You should not do this in PHP but either in the browser or in any other proper JavaScript interpreter.

Doing this in PHP may end up being something akin to building your own JavaScript parser.

My suggestion is:

  1. Install Node.js
  2. Run this PHP script:

     $obj = '{eventStart: "2018/12/01",eventEnd: "2018/12/01"}'; $cmd = "/usr/bin/node -e \\"var a = $obj; console.log(typeof a);\\""; $ph = proc_open($cmd, [ [ "pipe", "r" ], [ "pipe", "w" ], [ "pipe", "w" ] ], $pipes); $error = stream_get_contents($pipes[2]); $result = stream_get_contents($pipes[1]); fclose($pipes[0]); fclose($pipes[1]); fclose($pipes[2]); proc_close($ph); if (empty(trim($error)) && trim($result) == "object") { echo "Yay it's an object"; } else if (!empty($error)) { echo "Not valid JS syntax"; } else { echo "It's a ".trim($result)." instead of an object"; } 

Alternately just add the check in the browser like:

<?php
   $obj = '{eventStart: "2018/12/01",eventEnd: "2018/12/01"}';
?>
<script>
    try {
      var parameter = <?=$obj?>;
      if (typeof parameter !== 'object') {
          throw new Error('Not an object');
       }
    } catch (e) {
       //not valid
    }
 </script>

Note: I just realised that this may be an XY problem.

While {eventStart: "2018/12/01",eventEnd: "2018/12/01"} may be a valid JS object the best way to share JS objects cross platform is using JSON (which is short for JavaScript Object Notation). The best solution here is to ensure that whatever you pass to the client is a json encoded. For example:

<?php
   //Note how this is a PHP assoc array
   $obj = [ "eventStart" => "2018/12/01", "eventEnd" => "2018/12/01" ];
?>
<script>
    var object = <?= json_encode($obj); ?>;
</script>

try to parse on PHP side with json_decode . The method will return an array if it is valid. If not, then you will get false/null, as defined in the docs:

Returns the value encoded in json in appropriate PHP type. Values true, false and null are returned as TRUE, FALSE and NULL respectively. NULL is returned if the json cannot be decoded or if the encoded data is deeper than the recursion limit.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM