ropendev / phptojs
PHPToJS's class convert php variable's content to js variable's content preserving javascript expression (like function)
Installs: 4 711
Dependents: 1
Suggesters: 0
Security: 0
Stars: 11
Watchers: 3
Forks: 2
Open Issues: 1
Requires
- php: >=5.3.0
This package is auto-updated.
Last update: 2024-11-10 06:54:08 UTC
README
PHPToJS's class convert php variable's content to js variable's content preserving javascript expression (like function).
This class is perfect if you were limited by php function json_encode
, json's validity and/or JSON.parse
when you have a javascript function.
To Know : A php array with keys can't be a javascript array... so it will become a javascript object.
Installation
Composer Friendly (via packagist):
composer require ropendev/phptojs
Usage
You just need to call static function \rOpenDev\PHPToJS::render($mixed)
. Example :
use \rOpenDev\PHPToJS; $options = (object) array( 'title' => (object) array( 'label' => 'PHP To JS charts', 'class' => 'titlechart', 'formatter' => 'function(s) { return s.replace("-", "/"); }', ), 'data' => array(2014,2013,2012,2011) ); echo PHPToJS::render($options);
Will render :
{title:{label:"PHP To JS charts",class:"titlechart",formatter:function(s) { return s.replace("-", "/"); }},data:[2014,2013,2012,2011]}
You can use renderReadable
function which would render :
{
title: {
label: "PHP To JS charts",
class: "titlechart",
formatter: function(s) { return s.replace("-", "/"); }
},
data: [
2014,
2013,
2012,
2011
]
}
This function is available only in v1.0.0 !
Licence
MIT