= $len ? ( \substr( $haystack ,0 ,$len ) === $needle ) : false ); } // \darkphp\string\DwString::startWith2( $haystack ,$needle ); public static function startWith2( string $haystack ,string $needle) : bool { return $needle !== '' && strncmp( $haystack, $needle, strlen($needle) ) === 0; } // \darkphp\string\DwString::endsWith( $haystack ,$needle ); public static function endsWith( string $haystack ,string $needle ) : bool { $len = \strlen($needle); return $len == 0 || ( \strlen($haystack) >= $len ? ( \substr( $haystack ,( $len * -1 ) ) === $needle ) : false ); } /** * Converts a value to a valid file size (integer). * * Supports 'KB', 'MB' and 'GB' suffixes, where KB = 1024 B etc. * * The final value will be rounded to the nearest integer. * * Examples: * - '100' => 100 * - '100.12' => 100 * - '100KB' => 102400 * - '1.5MB' => 1572864 * * @param mixed $value File size (optionally with suffix). * @return integer Parsed file size. */ public static function toFileSizeEx( $value ) { if ( empty( $value ) ) { throw new DwException("Empty value cannot be converted to a file size."); } if ( \is_numeric( $value ) ) { return (integer) $value; } if ( ! \is_string( $value ) ) { throw new DwException("Given value [" . \var_export( $value ,true ) . "] cannot be converted to a file size."); } $str = \strtoupper( \trim( $value ) ); $count = \preg_match( '/^([0-9.]+)(KB|MB|GB)?$/' ,$str ,$matches ); if ( $count > 0 ) { $size = $matches[1]; $unit = $matches[2]; switch( $unit ) { case 'KB': $size *= \pow(1024, 1); break; case 'MB': $size *= \pow(1024, 2); break; case 'GB': $size *= \pow(1024, 3); break; case 'TB': $size *= \pow(1024, 4); break; } return (integer) $size; } throw new DwException( "Given value [$value] cannot be converted to a file size." ); } /** * Converts a value to string, or throws an exception if not possible. * * Objects can be converted to string if they implement the magic * __toString() method. * */ // \darkphp\string\DwString::toStringEx public static function toStringEx( $value ) : string { if ( \is_string( $value ) ) { return $value; } if ( \is_numeric( $value ) ) { return (string) $value; } if ( \is_object( $value ) ) { if ( \method_exists( $value ,'toString' ) ) return (string) $value->toString(); elseif ( \method_exists( $value ,'__toString' ) ) return (string) $value; else return (string) get_class( $value ); } else return gettype( $value ); throw new DwException("Given value [" . var_export($value, true) . "] cannot be converted to string."); } /** * Performs value substitution for string options. * * An option can contain PHP constants delimited by '${' and '}'. * * E.g. for input string "some ${FOO} value", the method will attempt * to substitute ${FOO} with the value of constant FOO if it exists. * * Therefore, if FOO is a constant, and it has value "bar", the resulting * string will be "some bar value". * * If the constant is not defined, it will be replaced by an empty string, * and the resulting string will be "some value". * * @param string $string String on which to perform substitution. * @return string */ // \darkphp\string\DwString::substConstants( $string ); public static function substConstants( $string ) { \preg_match_all('/\${([^}]+)}/', $string, $matches); foreach($matches[1] as $key => $match) { $match = \trim($match); $search = $matches[0][$key]; $replacement = \defined($match) ? \constant($match) : ''; $string = \str_replace($search, $replacement, $string); } return $string; } /** String values which are converted to boolean TRUE. */ private static $trueValues = array('1', 'true', 'yes', 'on'); /** * String values which are converted to boolean FALSE. * * Note that an empty string must convert to false, because * parse_ini_file() which is used for parsing configuration * converts the value _false_ to an empty string. */ private static $falseValues = array('0', 'false', 'no', 'off', ''); /** Converts $value to boolean, or throws an exception if not possible. */ // \darkphp\string\DwString::toBooleanEx( $string ); public static function toBooleanEx($value) { if (isset($value)) { if (is_bool($value)) { return $value; } $value = strtolower(trim($value)); if (in_array($value, self::$trueValues)) { return true; } if (in_array($value, self::$falseValues)) { return false; } } throw new DwException("Given value [" . var_export($value, true) . "] cannot be converted to boolean."); } /** * Converts $value to integer, or throws an exception if not possible. * Floats cannot be converted to integer. */ // \darkphp\string\DwString::toIntegerEx( $integer ); public static function toIntegerEx($value) { if (is_integer($value)) { return $value; } if (is_numeric($value) && ($value == (integer) $value)) { return (integer) $value; } throw new DwException("Given value [" . var_export($value, true) . "] cannot be converted to integer."); } /** * Converts $value to integer, or throws an exception if not possible. * Floats cannot be converted to integer. */ // \darkphp\string\DwString::toPositiveIntegerEx( $integer ); public static function toPositiveIntegerEx($value) { if (is_integer($value) && $value > 0) { return $value; } if (is_numeric($value) && ($value == (integer) $value) && $value > 0) { return (integer) $value; } throw new DwException("Given value [" . var_export($value, true) . "] cannot be converted to a positive integer."); } //const regExpExplodeByTwoNewLine = '(?s)((?<=(:?\r?\n)).*?(?=(:?\r?\n\r?\n)))'; const regExpExplodeByTwoNewLine = '(?s)((?<='.\PHP_EOL.').*?(?='.\PHP_EOL.\PHP_EOL.'))'; // \darkphp\string\DwString::explodeByTwoNewLine( $data ); public static function explodeByTwoNewLine( string $data ) : ?array { $found=null; $matches = array(); if ( preg_match_all( '/'.self::regExpExplodeByTwoNewLine.'/m' ,$data."\n\n" ,$matches ) ) { $found = $matches[1]; } return $found; } const regExpFilterNumerique = '[^0-9]*([0-9]+)[^0-9]*'; // \darkphp\string\DwString::filterNumerique( $data ); public static function filterNumerique( string $data ) : ?string { $found=null; $matches = array(); if ( preg_match_all( '/'.self::regExpFilterNumerique.'/m' ,$data ,$matches ) ) { $found=""; foreach( $matches[1] as $subNumerique ) $found.=$subNumerique; } return $found; } // Perplexity Production : // \darkphp\string\DwString::cleanBase64( $string ); public static function cleanBase64(string $str ): string { // Supprime tout caractère non base64 et espace $str = preg_replace('/[^A-Za-z0-9+\/=]/', '', $str); /* // S'assurer que la longueur est multiple de 4, ajouter padding si nécessaire $remainder = strlen($str) % 4; if ($remainder > 0) { $str .= str_repeat('=', 4 - $remainder); } //*/ return $str; } // \darkphp\string\DwString::w1250_to_utf8( $string ); public static function w1250_to_utf8($text) { // map based on: // http://konfiguracja.c0.pl/iso02vscp1250en.html // http://konfiguracja.c0.pl/webpl/index_en.html#examp // http://www.htmlentities.com/html/entities/ $map = array( chr(0x8A) => chr(0xA9) ,chr(0x8C) => chr(0xA6) ,chr(0x8D) => chr(0xAB) ,chr(0x8E) => chr(0xAE) ,chr(0x8F) => chr(0xAC) ,chr(0x9C) => chr(0xB6) ,chr(0x9D) => chr(0xBB) ,chr(0xA1) => chr(0xB7) ,chr(0xA5) => chr(0xA1) ,chr(0xBC) => chr(0xA5) ,chr(0x9F) => chr(0xBC) ,chr(0xB9) => chr(0xB1) ,chr(0x9A) => chr(0xB9) ,chr(0xBE) => chr(0xB5) ,chr(0x9E) => chr(0xBE) ,chr(0x80) => '€' ,chr(0x82) => '‚' ,chr(0x84) => '„' ,chr(0x85) => '…',chr(0x86) => '†' ,chr(0x87) => '‡' ,chr(0x89) => '‰' ,chr(0x8B) => '‹' ,chr(0x91) => '‘' ,chr(0x92) => '’' ,chr(0x93) => '“' ,chr(0x94) => '”' ,chr(0x95) => '•' ,chr(0x96) => '–' ,chr(0x97) => '—' ,chr(0x99) => '™' ,chr(0x9B) => '’' ,chr(0xA6) => '¦' ,chr(0xA9) => '©' ,chr(0xAB) => '«' ,chr(0xAE) => '®' ,chr(0xB1) => '±' ,chr(0xB5) => 'µ' ,chr(0xB6) => '¶' ,chr(0xB7) => '·' ,chr(0xBB) => '»' ); return \html_entity_decode( \mb_convert_encoding( \strtr( $text ,$map ), 'UTF-8', 'ISO-8859-2') , ENT_QUOTES ,'UTF-8' ); } // \darkphp\string\DwString::floatMicroSecondePrint( $float ); public static function floatMicroSecondePrint( float $floatMicroSecondes ) : string { $print=""; if ( 1000 > $floatMicroSecondes ) $print=number_format( \round( $floatMicroSecondes ,2 ) ,0 ,',' ,' ' )." us"; else { $floatMilliSecondes = \round( $floatMicroSecondes / 1000 ); if ( 1000 > $floatMilliSecondes ) $print = number_format( \round( $floatMilliSecondes ,2 ) ,0 ,',' ,' ' )." ms"; else { $floatSecondes = \round( $floatMilliSecondes / 1000 ); if ( 60 > $floatSecondes ) $print = number_format( \round( $floatSecondes ,2 ) ,0 ,',' ,' ' )." s"; else $print = number_format( \round( $floatSecondes / 60 ,2 ) ,0 ,',' ,' ' )." min"; } } return $print; } } ?>