Shortcode
1 month ago
Alphabet.php
1 month ago
Extension.php
1 month ago
Grouping.php
1 month ago
GutenBlock.php
1 month ago
Indices.php
1 month ago
Numbers.php
1 month ago
Query.php
1 month ago
Shortcode.php
1 month ago
Singleton.php
1 month ago
Strings.php
1 month ago
Strings.php
86 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 | if ( extension_loaded( 'mbstring' ) || class_exists( '\\Symfony\\Polyfill\\Mbstring\\Mbstring' ) ) { |
| 30 | return array_map( |
| 31 | /** |
| 32 | * Closure to extract a multibyte character from a string |
| 33 | * |
| 34 | * @return false|string |
| 35 | */ |
| 36 | function ( int $i ) use ( $string ) { |
| 37 | return mb_substr( $string, $i, 1 ); |
| 38 | }, |
| 39 | range( 0, mb_strlen( $string ) - 1 ) |
| 40 | ); |
| 41 | } else { |
| 42 | return str_split( $string ); |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * Perform a multibyte substring operation if mbstring is loaded, else use substr. |
| 48 | * |
| 49 | * @since 2.1.0 |
| 50 | * @param string $string The string to extract the substring from. |
| 51 | * @param int $start Start the substring at this character number (starts at zero). |
| 52 | * @param int $length Number of characters to include in the substring. |
| 53 | * @return string Substring of $string starting at $start with length $length characters. |
| 54 | */ |
| 55 | public static function maybe_mb_substr( string $string, int $start, int $length ): string { |
| 56 | if ( extension_loaded( 'mbstring' ) || class_exists( '\\Symfony\\Polyfill\\Mbstring\\Mbstring' ) ) { |
| 57 | return mb_substr( $string, $start, $length, 'UTF-8' ); |
| 58 | } else { |
| 59 | return substr( $string, $start, $length ); |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Perform a multibyte split operation if mbstring is loaded, else use explode. |
| 65 | * |
| 66 | * @param string $separator The character to mark each field. |
| 67 | * @param [type] $value The multibyte string to explode. |
| 68 | * @return array<string> The fields extracted from $value by exploding. |
| 69 | */ |
| 70 | public static function maybe_mb_split( string $separator, $value = null ): array { |
| 71 | $result = array(); |
| 72 | if ( is_string( $value ) ) { |
| 73 | if ( extension_loaded( 'mbstring' ) || class_exists( '\\Symfony\\Polyfill\\Mbstring\\Mbstring' ) ) { |
| 74 | $result = mb_split( $separator, $value ); |
| 75 | } else { |
| 76 | $result = explode( $separator, $value ); |
| 77 | } |
| 78 | } elseif ( is_array( $value ) ) { |
| 79 | $result = $value; |
| 80 | } |
| 81 | $result = array_map( 'trim', $result ); |
| 82 | $result = array_filter( $result ); |
| 83 | return $result; |
| 84 | } |
| 85 | } |
| 86 |