[英]Including JavaScript in PHP's json_encode()
I need to generate the following script using PHP and json_encode(). 我需要使用PHP和json_encode()生成以下脚本。
var obj= {
o1:123,
o2: {
o3: 456,
o4: function() {return $( "#myID" ).val();}
}
}
My attempt to do so is as follows. 我的尝试如下。
<?php
$a=array(
'o1'=>123,
'o2'=>array(
'o3'=>456,
'o4'=>'function() {return $( "#myID" ).val();}'
)
);
$json=json_encode($a);
?>
<script type="text/javascript">
<?php echo("var obj={$json};");?>
console.log(obj);
</script>
The resultant output is as follows. 结果输出如下。 The quotes around the properties poses no issues, however, the quotes around the JavaScript renders it as a string and not JavaScript.
属性周围的引号没有问题,但是,JavaScript周围的引号将其呈现为字符串而不是JavaScript。 I obviously can't not quote the JavaScript in the array as it will result in malformed JSON.
我显然不能在数组中引用JavaScript,因为它会导致JSON格式错误。
How can I include JavaScript in PHP's json_encode()? 如何在JavaScript的json_encode()中包含JavaScript?
var obj={
"o1":123,
"o2":{
"o3":456,
"o4":"function() {return $( \"#username\" ).val();}"
}
};
JSON does not support functions. JSON不支持功能。 Its a data interchange format.
它是一种数据交换格式。 Perhaps you should send some configuration data down and have a function change its behavior on that.
也许您应该向下发送一些配置数据,并通过一个函数更改其行为。 Or since you aren't doing it as an AJAX call at this point, just echo it out:
或由于此时您尚未将其作为AJAX调用进行处理,因此只需将其回显即可:
<script type="text/javascript">
<?php echo("var obj={$json};");?>
<?php echo("obj.myFunc = function..."); ?>
console.log(obj);
</script>
How about removing the quotations that surround the function? 如何删除函数周围的引号?
<?php
$obj = array(
'o1' => 123,
'o2' => array(
'o3' => 456,
'o4' => 'function() {return $( "#myID" ).val();}',
'o5' => 'function(param) {return $( "#myID" ).val();}'
)
);
$json = json_encode($obj);
while ($func = strpos($json, '":"function(')) {
$json = substr($json, 0, $func + 2) . substr($json, $func + 3);
while ($quote = strpos($json, '"', $func + 2)) {
$func = $quote + 1;
if (substr($json, $quote - 1, 1) == "\\") {
$json = substr($json, 0, $quote - 1) . substr($json, $quote);
continue;
}
$json = substr($json, 0, $quote) . substr($json, $quote + 1);
break;
}
}
echo $json;
This checks if the string starts with function(
, and if it is, removes the double quotes. 这将检查字符串是否以
function(
开头,如果是,则删除双引号。
The result is a JSON (but can still be used as a JavaScript object): 结果是JSON (但仍可以用作JavaScript对象):
{"o1":123,"o2":{"o3":456,"o4":function() {return $( "#myID" ).val();},"o5":function(param) {return $( "#myID" ).val();}}}
Upon setting this object to a variable, you can see that the function registered fine. 将此对象设置为变量后,您可以看到该函数已正确注册。
With that, you can still use the same technique you used before: 这样,您仍然可以使用以前使用的相同技术:
<script type="text/javascript">
<?php echo("var obj={$json};");?>
console.log(obj);
</script>
This is what I ended up doing. 这就是我最终要做的。 Kind of hackish, and I expect Dave's is a better solution, but haven't yet tested it.
有点骇人听闻,我希望Dave是更好的解决方案,但尚未对其进行测试。 I will do so.
我会照办的。
<?php
class fubar {
public function json_encode_JS($array)
{
$array=$this->_json_encode_JS($array);
$json=json_encode($array);
$json=str_replace(array('"%','%"'), '', $json);
$json=str_replace('\"', '"', $json);
return $json;
}
private function _json_encode_JS($array)
{
foreach($array as $key => $value){
if(is_array($value)){$array[$key]=$this->_json_encode_JS($value);}
else {
if(strpos($value, 'function(')===0){
$array[$key]='%'.$value.'%';
}
}
}
return $array;
}
}
$array=array(
'o1'=>123,
'o2'=>'function() {return $("#myID").val();}',
'o3'=>array(
'o4'=>456,
'o5'=>'function() {alert("Hello why don\'t you answer "+$("#myID").val());}'
)
);
?>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.js" type="text/javascript"></script>
<script type="text/javascript">
<?php
$obj=new fubar();
echo('var obj='.$obj->json_encode_JS($array).';');
?>
console.log(obj);
obj.o3.o5();
</script>
<input id="myID" name="myID" type="text" value="hello">
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.