How to check if a string is a JavaScript command with PHP
i need a solution to check a string with PHP
and determine if it is a javascript
command.
i want to generate javascript scripts dynamically in runtime. but a string could be an invalid javascript command.
for example in :
$str = 'hello';
hello
is a simple string and not a javascript command but in:
$str = 'str.split()';
str.split()
is javascript commnad.
i want to know if a string开发者_StackOverflow is really a javascript command
I think you should try using a JavaScript linter written in PHP.
Sure the JavaScript will need to be valid but it could work.
Just check if the linter says it's okay and then you can consider it as JavaScript code.
Edit: Example taken and edited from the page I linked to.
$lint = new JSLEngine();
if ($lint->Lint($js))
{
// This is valid JavaScript, now we can do stuff with it.
}
You could always create an array of every javascript function then use in_array($variable, $array)
to check whether the specified variable is in that array, however this would mean writing every single javascript function manually.
The problem you will have is highlighted in the thread below:
How to display all methods of an object in Javascript?
I hope you get your problem resolved.
Just to extend on this - How to display all methods of an object in Javascript?
This post explains how ES5 will allow you to do this. However you will need to find a way of getting the javascript content into a php variable, eg <?php $blah = get_file_contents("thejavascriptpage.php");?>
- thejavascriptpage.php containing the javascript, then explode the arrayed response.
精彩评论