「PHP」- 常用函数库

<?php

/**
 * 移除地址名中的「市」或者「区」
 * @param type $province
 * @return string
 */
public static function removeAddressTail($address)
{

    if (self::endsWith($address, "特别行政区")) {

        return mb_str_replace("特别行政区", "", $address);
    } elseif (self::endsWith($address, "自治区")) {

        return mb_str_replace("自治区", "", $address);
    } elseif (self::endsWith($address, "省")) {

        return mb_substr($address, 0, mb_strlen($address) - 1);
    } elseif (self::endsWith($address, "市")) {

        return mb_substr($address, 0, mb_strlen($address) - 1);
    }

    return $address;
}

/**
 * 检查$haystack是否以$needle开头
 * @param string $haystack
 * @param string $needle
 * @return boolean
 */
private static function startsWith($haystack, $needle)
{
    $length = mb_strlen($needle);
    return (mb_substr($haystack, 0, $length) === $needle);
}

/**
 * 检查字符串$haystack是否以$needle结尾
 * @param string $haystack
 * @param string $needle
 * @return boolean
 */
private static function endsWith($haystack, $needle)
{
    $length = mb_strlen($needle);

    return $length === 0 ||
            (mb_substr($haystack, -$length) === $needle);
}

/**
 * var_export函数扩展,导出结果为方括号
 * @param type $var
 * @param type $indent
 * @return type
 */
function var_export54($var, $indent = "")
{
    switch (gettype($var)) {
        case "string":
            return '"' . addcslashes($var, "\\\$\"\r\n\t\v\f") . '"';
        case "array":
            $indexed = array_keys($var) === range(0, count($var) - 1);
            $r = [];
            foreach ($var as $key => $value) {
                $r[] = "$indent    "
                        . ($indexed ? "" : var_export54($key) . " => ")
                        . var_export54($value, "$indent    ");
            }
            return "[\n" . implode(",\n", $r) . "\n" . $indent . "]";
        case "boolean":
            return $var ? "TRUE" : "FALSE";
        default:
            return var_export($var, TRUE);
    }
}