php字符串

字符串截取

/字符串是否存在某字符
strstr($data['tags'], ',')
//字符转数组
$tagsArr = explode(", ", $data['tags']);

//截取某子字符串前的字符串
if (!function_exists("getBeforeString")) {
    function getBeforeString($string, $beforeString)
    {
        return substr($string, 0, strpos($string, $beforeString));
    }
}

//截取某子字符串后的字符串
if (!function_exists("getLaterString")) {
    function getLaterString($string, $laterString)
    {
        return substr($string, (stripos($string, $laterString) + strlen($laterString)));
    }
}

//字符串去除左右空格
if (!function_exists("removeLRSpaceString")) {
    function removeLRSpaceString($string)
    {
        $string = rtrim($string, ' ');
        return ltrim($string, ' ');
    }
}

字符串 – PHP 工具类

= 65 && $firstchar_ord <= 91)
            || ($firstchar_ord >= 48 && $firstchar_ord <= 57)) {
            return $s0{0};
        }
        $s   = mb_convert_encoding($s0, 'GB2312', 'UTF-8');
        $asc = ord($s{0}) * 256 + ord($s{1}) - 65536;
        if ($asc >= -20319 && $asc <= -20284) return "A";
        if ($asc >= -20283 && $asc <= -19776) return "B";
        if ($asc >= -19775 && $asc <= -19219) return "C";
        if ($asc >= -19218 && $asc <= -18711) return "D";
        if ($asc >= -18710 && $asc <= -18527) return "E";
        if ($asc >= -18526 && $asc <= -18240) return "F";
        if ($asc >= -18239 && $asc <= -17923) return "G";
        if ($asc >= -17922 && $asc <= -17418) return "H";
        if ($asc >= -17417 && $asc <= -16475) return "J";
        if ($asc >= -16474 && $asc <= -16213) return "K";
        if ($asc >= -16212 && $asc <= -15641) return "L";
        if ($asc >= -15640 && $asc <= -15166) return "M";
        if ($asc >= -15165 && $asc <= -14923) return "N";
        if ($asc >= -14922 && $asc <= -14915) return "O";
        if ($asc >= -14914 && $asc <= -14631) return "P";
        if ($asc >= -14630 && $asc <= -14150) return "Q";
        if ($asc >= -14149 && $asc <= -14091) return "R";
        if ($asc >= -14090 && $asc <= -13319) return "S";
        if ($asc >= -13318 && $asc <= -12839) return "T";
        if ($asc >= -12838 && $asc <= -12557) return "W";
        if ($asc >= -12556 && $asc <= -11848) return "X";
        if ($asc >= -11847 && $asc <= -11056) return "Y";
        if ($asc >= -11055 && $asc <= -10247) return "Z";
        return null;
    }

    /**
     * 判断是否包含特殊字符
     *
     * @param $str
     * @return bool
     */
    public static function haveSpecialChar($str)
    {
        $length = mb_strlen($str);
        $array  = [];
        for ($i = 0; $i < $length; $i++) {
            $array[] = mb_substr($str, $i, 1, 'utf-8');
            if (strlen($array[$i]) >= 4) {
                return true;

            }
        }
        return false;
    }

    /**
     * 获取去掉空格的字符串
     *
     * @param $str
     * @return mixed
     */
    public static function getNotEmptyString($str)
    {
        $str = trim($str);
        $str = str_replace(" ", "", $str);
        return str_replace(" ", "", $str);
    }

    /**
     * 字符串压缩解压
     *
     * @param string $str
     * @param bool   $isZip
     * @return string
     */
    public static function strZipUnZip(string $str, bool $isZip = true)
    {
        if ($isZip) {
            return static::stringZip($str);
        } else {
            return static::stringUnzip($str);
        }
    }

    /**
     * 字符串压缩
     *
     * @param string $str
     * @return string
     */
    public static function stringZip(string $str)
    {
        if (empty($str)) {
            return $str;
        }
        return base64_encode(gzencode($str));
    }

    /**
     * 字符串解压缩
     *
     * @param string $str
     * @return string
     */
    public static function stringUnzip(string $str)
    {
        if (empty($str)) {
            return $str;
        }
        return gzdecode(base64_decode($str));
    }

    public static function stringSpecialChar(string $str)
    {
        // /[\'.,:;*?~`!@#$%^&+=)(<>{}]|\]|\[|\/|\\\|"|\|/
        if (preg_match("/[\'.,:;*?~`!@#$%^&+=<>{}]|\]|\[|\|\\\|\"|\|/", $str)) {
            return false;
        }
        return true;
    }

    /**
     * 将字符串进行处理,中间用星号表示
     *
     * @param $str
     * @return mixed|string
     */
    public static function substrCut($str, $len = 1)
    {
        //获取字符串长度
        $strlen = mb_strlen($str, 'utf-8');
        //如果字符创长度小于2,不做任何处理
        if ($strlen < 2) {
            return $str;
        } else {
            //mb_substr — 获取字符串的部分
            $firstStr = mb_substr($str, 0, $len, 'utf-8');
            $lastStr  = mb_substr($str, -1, $len, 'utf-8');
            //str_repeat — 重复一个字符串
            return $strlen == 2 ? $firstStr . str_repeat('*', mb_strlen($str, 'utf-8') - 1) : $firstStr . str_repeat("*", $strlen - 2) . $lastStr;
        }
    }
}

Buy me a cup of coffee 🙂

觉得对你有帮助,就给我打赏吧,谢谢!

微信赞赏码链接,点击跳转:

php字符串插图

Tags: