Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
20.00% covered (danger)
20.00%
2 / 10
0.00% covered (danger)
0.00%
0 / 1
CRAP
0.00% covered (danger)
0.00%
0 / 1
AbstractString
20.00% covered (danger)
20.00%
2 / 10
0.00% covered (danger)
0.00%
0 / 1
4.05
0.00% covered (danger)
0.00%
0 / 1
 isUTF8
20.00% covered (danger)
20.00%
2 / 10
0.00% covered (danger)
0.00%
0 / 1
4.05
1<?php
2
3/**
4 * Abstract parent for the string wrapper classes.
5 *
6 * This Source Code Form is subject to the terms of the Mozilla Public License,
7 * v. 2.0. If a copy of the MPL was not distributed with this file, You can
8 * obtain one at https://mozilla.org/MPL/2.0/.
9 *
10 * @package   phpMyFAQ
11 * @author    Anatoliy Belsky <ab@php.net>
12 * @copyright 2009-2026 phpMyFAQ Team
13 * @license   https://www.mozilla.org/MPL/2.0/ Mozilla Public License Version 2.0
14 * @link      https://www.phpmyfaq.de
15 * @since     2009-04-06
16 */
17
18declare(strict_types=1);
19
20namespace phpMyFAQ\Strings;
21
22/**
23 * Class StringsAbstract
24 *
25 * @package phpMyFAQ\Strings
26 */
27abstract class AbstractString
28{
29    /**
30     * Default encoding.
31     *
32     * @var string
33     */
34    public const DEFAULT_ENCODING = 'utf-8';
35
36    /**
37     * Default language.
38     */
39    public const string DEFAULT_LANGUAGE = 'en';
40
41    /**
42     * Encoding.
43     */
44    protected string $encoding = self::DEFAULT_ENCODING;
45
46    /**
47     * Language.
48     */
49    protected string $language = self::DEFAULT_LANGUAGE;
50
51    /**
52     * Check if the string is a Unicode string.
53     *
54     * @param string $str String
55     */
56    public static function isUTF8(string $str): bool
57    {
58        if (function_exists('mb_detect_encoding')) {
59            return (bool) mb_detect_encoding($str, self::DEFAULT_ENCODING, strict: true);
60        }
61
62        $regex =
63            '/^([\x00-\x7f]|[\xc2-\xdf][\x80-\xbf]|\xe0[\xa0-\xbf][\x80-\xbf]|[\xe1-\xec][\x80-\xbf]{2}|'
64            . '\xed[\x80-\x9f][\x80-\xbf]|'
65            . '[\xee-\xef][\x80-\xbf]{2}|'
66            . '\xf0[\x90-\xbf][\x80-\xbf]{2}|'
67            . '[\xf1-\xf3][\x80-\xbf]{3}|'
68            . '\xf4[\x80-\x8f][\x80-\xbf]{2})*$/';
69
70        return preg_match($regex, $str) === 1;
71    }
72}