X-Git-Url: http://git.polytechnique.org/?a=blobdiff_plain;ds=sidebyside;f=include%2Fmisc.inc.php;h=27ee64258b56a1ea7c7fef7aea634ba518773a92;hb=fa7ffd661d77b24cdb385aca7bdb04c938214061;hp=3615bcbd7a6b72896a73bdfccedb1e83c2ed786e;hpb=7e001f68b7928d80ccf32b8a8bca55d304476d3e;p=platal.git diff --git a/include/misc.inc.php b/include/misc.inc.php index 3615bcb..27ee642 100644 --- a/include/misc.inc.php +++ b/include/misc.inc.php @@ -1,6 +1,6 @@ $errors) { + return null; + } return $d; } catch (Exception $e) { return null; @@ -323,5 +334,69 @@ function format_datetime($date, $format) // } } -// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: +/** 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) || ($data[0] == '-' && ctype_digit(substr($data, 1))); +} + +/** 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 fenc=utf-8: ?>