PluginProbe ʕ •ᴥ•ʔ
AlphaListing / 4.5.0
AlphaListing v4.5.0
4.5.0 trunk 4.3.4 4.3.5 4.3.6 4.3.7 4.4.0
alphalisting / src / Strings.php
alphalisting / src Last commit date
Shortcode 2 weeks ago Alphabet.php 2 weeks ago Extension.php 2 weeks ago Grouping.php 2 weeks ago GutenBlock.php 2 weeks ago Indices.php 2 weeks ago Numbers.php 2 weeks ago Query.php 2 weeks ago Shortcode.php 2 weeks ago Singleton.php 2 weeks ago Strings.php 2 weeks ago
Strings.php
92 lines
1 <?php
2 /**
3 * String functions
4 *
5 * @package alphalisting
6 */
7
8 declare(strict_types=1);
9
10 namespace eslin87\AlphaListing;
11
12 if ( ! defined( 'ABSPATH' ) ) {
13 exit;
14 }
15
16 /**
17 * String functions
18 */
19 class Strings {
20 /**
21 * Split a multibyte string into an array. (see https://php.net/manual/en/function.mb-split.php#121330)
22 *
23 * @since 1.0.0
24 * @since 2.0.0 moved from Query class to standalone function
25 * @param string $string multi-byte string.
26 * @return array<int,string> individual multi-byte characters from the string
27 */
28 public static function mb_string_to_array( string $string ): array {
29 // Guard the empty string: range( 0, -1 ) counts *downwards* and yields
30 // array( 0, -1 ), so without this the function returns two empty strings.
31 if ( '' === $string ) {
32 return array();
33 }
34
35 if ( extension_loaded( 'mbstring' ) || class_exists( '\\Symfony\\Polyfill\\Mbstring\\Mbstring' ) ) {
36 return array_map(
37 /**
38 * Closure to extract a multibyte character from a string
39 *
40 * @return false|string
41 */
42 function ( int $i ) use ( $string ) {
43 return mb_substr( $string, $i, 1 );
44 },
45 range( 0, mb_strlen( $string ) - 1 )
46 );
47 } else {
48 return str_split( $string );
49 }
50 }
51
52 /**
53 * Perform a multibyte substring operation if mbstring is loaded, else use substr.
54 *
55 * @since 2.1.0
56 * @param string $string The string to extract the substring from.
57 * @param int $start Start the substring at this character number (starts at zero).
58 * @param int $length Number of characters to include in the substring.
59 * @return string Substring of $string starting at $start with length $length characters.
60 */
61 public static function maybe_mb_substr( string $string, int $start, int $length ): string {
62 if ( extension_loaded( 'mbstring' ) || class_exists( '\\Symfony\\Polyfill\\Mbstring\\Mbstring' ) ) {
63 return mb_substr( $string, $start, $length, 'UTF-8' );
64 } else {
65 return substr( $string, $start, $length );
66 }
67 }
68
69 /**
70 * Perform a multibyte split operation if mbstring is loaded, else use explode.
71 *
72 * @param string $separator The character to mark each field.
73 * @param string|array|null $value The multibyte string to explode, or an array to pass through.
74 * @return array<string> The fields extracted from $value by exploding.
75 */
76 public static function maybe_mb_split( string $separator, $value = null ): array {
77 $result = array();
78 if ( is_string( $value ) ) {
79 if ( extension_loaded( 'mbstring' ) || class_exists( '\\Symfony\\Polyfill\\Mbstring\\Mbstring' ) ) {
80 $result = mb_split( $separator, $value );
81 } else {
82 $result = explode( $separator, $value );
83 }
84 } elseif ( is_array( $value ) ) {
85 $result = $value;
86 }
87 $result = array_map( 'trim', $result );
88 $result = array_filter( $result );
89 return $result;
90 }
91 }
92