<?php
/**
* @file utility.php
*
*
* @brief Utility functions
*/
/**
* @fn Compute the difference between two microtimes
*
* From discussion at <A HREF="http://uk2.php.net/microtime">http://uk2.php.net/microtime</A>
*
* @param a first time
* @param b second time
*/
function microtime_diff($a, $b) {
list($a_dec, $a_sec) = explode(" ", $a);
list($b_dec, $b_sec) = explode(" ", $b);
return $b_sec - $a_sec + $b_dec - $a_dec;
}
/**
* @fn Converts a PHP string to a format acceptable as a Javascript variable.
*
* Replaces all line breaks with \n or \r.
* From alexandre at dutra-cancado dot net at <A HREF="http://www.php.net/manual/en/function.nl2br.php">
* nl2br</A>
*
* @param php_string PHP string
* @return Javascript compatible string
*
*/
function js_string ($php_string)
{
return preg_replace(
'/([\r\n])/e',
"ord('$1')==10?'\\n':'\\r'",
$php_string
);
}
/**
* @fn Converts a PHP string to a format acceptable as a Javascript variable.
*
* Replaces all line breaks with \n or \r.
* From alexandre at dutra-cancado dot net at <A HREF="http://www.php.net/manual/en/function.nl2br.php">
* nl2br</A>
*
* @param php_string PHP string
* @return Javascript compatible string
/**
* @fn Format a file name to be safe
*
* Taken from the PEAR HTTP_Upload package
*
* @param string $file The string file name
* @param int $maxlen Maximun permited string lenght
* @return string Formatted file name
*/
function nameToSafe($name, $maxlen=250)
{
$noalpha = 'ÁÉÍÓÚÝáéíóúýÂÊÎÔÛâêîôûÀÈÌÒÙàèìòùÄËÏÖÜäëïöüÿÃãÕõÅåÑñÇç@°ºª';
$alpha = 'AEIOUYaeiouyAEIOUaeiouAEIOUaeiouAEIOUaeiouyAaOoAaNnCcaooa';
$name = substr($name, 0, $maxlen);
$name = strtr($name, $noalpha, $alpha);
// not permitted chars are replaced with "_"
return preg_replace('/[^a-zA-Z0-9,._\+\()\-]/', '_', $name);
}
?>