95 lines
1.9 KiB
PHP
Executable File
95 lines
1.9 KiB
PHP
Executable File
<?php
|
|
|
|
interface PhpizeInterface {
|
|
|
|
public function __toPhp( $compact = false );
|
|
|
|
}
|
|
|
|
function html ($a, $encoding = 'UTF-8', $flag = null ) {
|
|
|
|
if ( is_null($flag) )
|
|
$flag = ENT_COMPAT;
|
|
|
|
return htmlentities($a, $flag, $encoding);
|
|
|
|
}
|
|
|
|
function phpize( $data, $file = null, $compact = false ) {
|
|
|
|
if ( is_array($data) ) {
|
|
|
|
$str = $compact ? '' : "\n";
|
|
$php = "array(";
|
|
|
|
foreach ( $data as $k => $v ) {
|
|
|
|
if ( !is_numeric($k) )
|
|
$k = "'" . addslashes($k) . "'";
|
|
|
|
$php .= $str . $k . " => " . phpize($v, false, $compact) . ",";
|
|
|
|
}
|
|
|
|
if ( !$compact )
|
|
$php = preg_replace("!\n!", "\n\t", $php);
|
|
|
|
$php .= $str . ")";
|
|
|
|
} elseif ( is_numeric($data) )
|
|
$php = addslashes($data);
|
|
|
|
elseif ( is_bool($data) )
|
|
$php = $data ? 'true' : 'false';
|
|
|
|
elseif ( is_null($data) )
|
|
$php = "null";
|
|
|
|
elseif ( is_string($data) )
|
|
$php = "'" . implode('\' . "\n" . \'', explode("\n", preg_replace(
|
|
array('!\\\\!', '!\'!'),
|
|
array('\\\\\\', '\\\''),
|
|
$data
|
|
))) . "'";
|
|
|
|
elseif ( $data instanceof PhpizeInterface )
|
|
$php = $data->__toPhp($compact);
|
|
|
|
else throw new Exception('Object does not implements PhpizeInterface : ' . get_class($data));
|
|
|
|
if ( $file )
|
|
file_put_contents($file, "<?php\n\nreturn " . $php . "\n\n?>");
|
|
|
|
return $php;
|
|
|
|
}
|
|
|
|
function sprinto( $str, $obj, $default = '') {
|
|
|
|
$a = function ($matches) use ($obj, $default, &$set) {
|
|
|
|
$data = (array) $obj;
|
|
$path = preg_split('!\.!', $matches[1]);
|
|
foreach ( $path as $step )
|
|
if ( array_key_exists($step, $data) )
|
|
$data = $data[$step];
|
|
else $data = '';
|
|
|
|
return $data;
|
|
return !empty($data) ? $data : ( $default === null ? $matches[0] : $default );
|
|
};
|
|
|
|
return preg_replace_callback("!\{([a-zA-Z0-9_:\-\.]*)\}!", $a, $str);
|
|
|
|
}
|
|
|
|
function joinstr ( $array, $glue = ' ', $start = '', $end = '' ) {
|
|
|
|
if ( $str = implode($glue, $array) )
|
|
return $start . $str . $end;
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
?>
|