Hopefully this will make peoples lives easier
EDIT: Better code further down
Javascript
Code: Select all
/* Converts a string representation of a php-variable to a js variable */
function php2js (variable) {
var i, js_code;
/* Replace entities */
variable = variable.replace(/</g, "<");
variable = variable.replace(/>/g, ">");
variable = variable.replace(/&/g, "&");
variable = variable.replace(/"/g, '"');
variable = variable.replace(/'/g, "\\'");
variable = variable.replace(/&brkln;/g, "\n");
variable = variable.replace(/&slash;/g, "\\");
/* Create global tokens */
tokens = variable.split("\n");
/* Remove empty tokens */
for(i = 0; i < tokens.length; i++) {
if (tokens[i] == '') {
tokens.splice(i, 1);
}
}
/* Create js code */
js_code = 'var js_var = ' + build_js_var() + ';';
/* Evaluate code and return it */
eval(js_code);
return js_var;
}
/* Builds js_code from the tokens */
function build_js_var () {
var js, match, token;
token = tokens.shift();
if (token.match(/\w*? ?(Array|Object)/) != null) {
/* Match arrays and objects, convert both to js Array */
js = 'new Array';
} else if ((match = token.match(/\s*?(\(|\))/)) != null) {
/* Match parenthesis */
js = match[1];
} else if ((match = token.match(/\s+?\[.+?\] => (.*)/)) != null) {
/* Match array values and object properties */
js = "'" + match[1] + ((tokens[0].match(/\s*?\)/) != null) ? "'" : "',");
} else {
js = "'" + token + "'";
}
if (tokens.length > 0) {
js += build_js_var();
}
return js;
}
/* Dumps variable. Output is similar to a preformated print_r */
function print_f (variable, margin){
var property, html, tabs, i, key_color;
key_color = 'orange';
tabs = '';
for (i = 0; i < margin + 1; i++) {
tabs += '\t';
}
html = (margin == 0 && typeof(variable) == 'object') ? '<pre>Array\n(\n' : '';
if (margin == 0 && html == '') {
html += tabs + variable + '\n';
} else {
for(property in variable) {
if (typeof(variable[property]) == 'object') {
html += tabs + '[<span style="color:' + key_color + '">' + property + '</span>] => Array\n' +
tabs + '(\n' + print_f(variable[property], margin + 1) + tabs + ')\n';
} else if (typeof(variable[property]) == 'function') {
html += tabs + '[<span style="color:' + key_color + '">' + property + '</span>] => ' + variable[property].toSource() + '\n';
} else {
html += tabs + '[<span style="color:' + key_color + '">' + property + '</span>] => ' + variable[property] + '\n';
}
}
}
if (margin == 0) {
html += (typeof(variable) == 'object') ? ')</pre>' : '';
document.write(html);
document.close();
} else {
return html;
}
}
/* Test-function */
function test (php_string) {
/* Convert php string to js var */
var js_var = php2js(php_string);
/* Print js-variable */
print_f(js_var, 0);
}Code: Select all
/**
* Makes <var>$var</var> safe to pass as parameter to a js-function
* @param string Unsafe string-representation of variable
* @return string Safe string-representation of variable
*/
function make_js_safe ($var) {
/* Note: &brkln; and &slash; is not html entities */
return htmlspecialchars(str_replace(array("\n", "\\"), array('&brkln;', '&slash;'), $var), ENT_QUOTES);
}
/**
* Wrapper function for calling a javascript function
* @param string Name of the js function
* @param mixed Parameter(s) to pass to the js function
* @param bool Whether to create script-tags around the function-call
*/
function call_js_function ($fname, $var, $create_js_tags = true) {
$fcall = $fname . '("' . make_js_safe( print_r($var, 1) ) . '")';
echo ($create_js_tags) ? "<script type='text/javascript'>\n\t$fcall\n</script>" : $fcall;
}Code: Select all
/* Test php2js convertion with some more or less random data */
$o = new stdClass();
$o->g = 'something';
$o->prop = array(4 => 'fo\'o"', 'f\d' => -1);
$t = array(-3 => 1.2, 'bar' => 'images/alter2.JPG', 0 => false, 't' => null, 'key' => 'images/log1.JPG', 'o' => $o);
echo '<pre>';
print_r($t);
echo '</pre>';
call_js_function('test', $t);Code: Select all
Array
(
[-3] => 1.2
[bar] => images/alter2.JPG
[0] =>
[t] =>
[key] => images/log1.JPG
[o] => stdClass Object
(
[g] => something
[prop] => Array
(
[4] => fo'o"
[f\d] => -1
)
)
)
Array
(
[0] => 1.2
[1] => images/alter2.JPG
[2] =>
[3] =>
[4] => images/log1.JPG
[5] => Array
(
[0] => something
[1] => Array
(
[0] => fo'o"
[1] => -1
)
)
)regards tores