0

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
}
5
  • is it a valid object? Commented Jul 27, 2018 at 7:41
  • U mean is a valid JSON ? Commented Jul 27, 2018 at 7:41
  • If it's valid JSON, it'll produce a valid JS object when parsed. So if you can json_encode the php object into valid JSON, it'll be able to become a valid JS object. Commented Jul 27, 2018 at 7:44
  • I have edited the question please check @Shilly. Commented Jul 27, 2018 at 7:55
  • I don't understand the problem. Is the data(valid object here) not getting stringified correctly before it gets posted to the php? Commented Jul 27, 2018 at 8:00

2 Answers 2

1

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>
Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.