X-Git-Url: http://git.polytechnique.org/?a=blobdiff_plain;f=include%2Fmisc.inc.php;h=b60045ded495424c0400f9936bf28111572f68d2;hb=e92ecb8c24421ca1dd4f87ad7478d0d8277e1f60;hp=c1b8c6711d8ea60fbd757e7463af56c693db54c7;hpb=ef815e754cee390b7503d848cf753f0cef328d6b;p=platal.git diff --git a/include/misc.inc.php b/include/misc.inc.php index c1b8c67..b60045d 100644 --- a/include/misc.inc.php +++ b/include/misc.inc.php @@ -1,6 +1,6 @@ = PHP5.3) +if (!function_exists('quoted_printable_encode')) { + function quoted_printable_encode($input, $line_max = 76) + { + $lines = preg_split("/(?:\r\n|\r|\n)/", $input); + $eol = "\n"; + $linebreak = "=0D=0A=\n "; + $escape = "="; + $output = ""; - foreach ($lines as $j => $line) { - $linlen = strlen($line); - $newline = ""; - for($i = 0; $i < $linlen; $i++) { - $c = $line{$i}; - $dec = ord($c); - if ( ($dec == 32) && ($i == ($linlen - 1)) ) { - // convert space at eol only - $c = "=20"; - } elseif ( ($dec == 61) || ($dec < 32 ) || ($dec > 126) ) { - // always encode "\t", which is *not* required - $c = $escape.strtoupper(sprintf("%02x",$dec)); - } - if ( (strlen($newline) + strlen($c)) >= $line_max ) { // CRLF is not counted - $output .= $newline.$escape.$eol; - $newline = " "; - } - $newline .= $c; - } // end of for - $output .= $newline; - if ($j $line) { + $linlen = strlen($line); + $newline = ""; + for($i = 0; $i < $linlen; $i++) { + $c = $line{$i}; + $dec = ord($c); + if ( ($dec == 32) && ($i == ($linlen - 1)) ) { + // convert space at eol only + $c = "=20"; + } elseif ( ($dec == 61) || ($dec < 32 ) || ($dec > 126) ) { + // always encode "\t", which is *not* required + $c = $escape.strtoupper(sprintf("%02x",$dec)); + } + if ( (strlen($newline) + strlen($c)) >= $line_max ) { // CRLF is not counted + $output .= $newline.$escape.$eol; + $newline = " "; + } + $newline .= $c; + } // end of for + $output .= $newline; + if ($j $errors) { + return null; + } return $d; } catch (Exception $e) { return null; @@ -320,5 +334,69 @@ function format_datetime($date, $format) // } } +/** Get the first n characters of the string + */ +function left($string, $count) +{ + return substr($string, 0, $count); +} + +/** Get the last n characters of the string + */ +function right($string, $count) +{ + return substr($string, -$count); +} + +/** Check if a string is a prefix for another one. + */ +function starts_with($string, $prefix, $caseSensitive = true) +{ + $prefixLen = strlen($prefix); + if (strlen($string) < $prefixLen) { + return false; + } + $part = left($string, $prefixLen); + if ($caseSensitive) { + return strcmp($prefix, $part) === 0; + } else { + return strcasecmp($prefix, $part) === 0; + } +} + +/** Check if a string is a suffix for another one. + */ +function ends_with($string, $suffix, $caseSensitive = true) +{ + $suffixLen = strlen($suffix); + if (strlen($string) < $suffixLen) { + return false; + } + $part = right($string, $suffixLen); + if ($caseSensitive) { + return strcmp($suffix, $part) === 0; + } else { + return strcasecmp($suffix, $part) === 0; + } +} + +/** Check if the input data can be seen as an integer. + */ +function can_convert_to_integer($data) +{ + return is_int($data) || ctype_digit($data); +} + +/** Interpret the input data as an integer or return false. + */ +function to_integer($data) +{ + if (!can_convert_to_integer($data)) { + return false; + } + return intval($data); +} + + // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: ?>