Package | system.web.helpers |
---|---|
Inheritance | class CJavaScript |
Since | 1.0 |
Version | $Id$ |
Source Code | framework/web/helpers/CJavaScript.php |
Method | Description | Defined By |
---|---|---|
encode() | Encodes a PHP variable into javascript representation. | CJavaScript |
jsonDecode() | Decodes a JSON string. | CJavaScript |
jsonEncode() | Returns the JSON representation of the PHP data. | CJavaScript |
quote() | Quotes a javascript string. | CJavaScript |
public static string encode(mixed $value)
| ||
$value | mixed | PHP variable to be encoded |
{return} | string | the encoded string |
public static function encode($value)
{
if(is_string($value))
{
if(strpos($value,'js:')===0)
return substr($value,3);
else
return "'".self::quote($value)."'";
}
else if($value===null)
return 'null';
else if(is_bool($value))
return $value?'true':'false';
else if(is_integer($value))
return "$value";
else if(is_float($value))
{
if($value===-INF)
return 'Number.NEGATIVE_INFINITY';
else if($value===INF)
return 'Number.POSITIVE_INFINITY';
else
return "$value";
}
else if(is_object($value))
return self::encode(get_object_vars($value));
else if(is_array($value))
{
$es=array();
if(($n=count($value))>0 && array_keys($value)!==range(0,$n-1))
{
foreach($value as $k=>$v)
$es[]="'".self::quote($k)."':".self::encode($v);
return '{'.implode(',',$es).'}';
}
else
{
foreach($value as $v)
$es[]=self::encode($v);
return '['.implode(',',$es).']';
}
}
else
return '';
}
Encodes a PHP variable into javascript representation.
Example:
$options=array('key1'=>true,'key2'=>123,'key3'=>'value'); echo CJavaScript::encode($options); // The following javascript code would be generated: // {'key1':true,'key2':123,'key3':'value'}
public static mixed jsonDecode(string $data, boolean $useArray=true)
| ||
$data | string | the data to be decoded |
$useArray | boolean | whether to use associative array to represent object data |
{return} | mixed | the decoded PHP data |
public static function jsonDecode($data,$useArray=true)
{
if(function_exists('json_decode'))
return json_decode($data,$useArray);
else
return CJSON::decode($data,$useArray);
}
Decodes a JSON string.
public static string jsonEncode(mixed $data)
| ||
$data | mixed | the data to be encoded |
{return} | string | the JSON representation of the PHP data. |
public static function jsonEncode($data)
{
if(function_exists('json_encode'))
return json_encode($data);
else
{
return CJSON::encode($data);
}
}
Returns the JSON representation of the PHP data.
public static string quote(string $js, boolean $forUrl=false)
| ||
$js | string | string to be quoted |
$forUrl | boolean | whether this string is used as a URL |
{return} | string | the quoted string |
public static function quote($js,$forUrl=false)
{
if($forUrl)
return strtr($js,array('%'=>'%25',"\t"=>'\t',"\n"=>'\n',"\r"=>'\r','"'=>'\"','\''=>'\\\'','\\'=>'\\\\','</'=>'<\/'));
else
return strtr($js,array("\t"=>'\t',"\n"=>'\n',"\r"=>'\r','"'=>'\"','\''=>'\\\'','\\'=>'\\\\','</'=>'<\/'));
}
Quotes a javascript string. After processing, the string can be safely enclosed within a pair of quotation marks and serve as a javascript string.
Signup or Login in order to comment.