diff --git a/composer.json b/composer.json index d48b460..46d6986 100644 --- a/composer.json +++ b/composer.json @@ -15,14 +15,16 @@ ], "require": { "php": ">=5.3", - "ext-curl": "*", - "ext-mbstring": "*" + "ext-curl": "*" }, "require-dev": { "ext-gd": "*", "phpunit/phpunit": "*", "squizlabs/php_codesniffer": "*" }, + "suggest": { + "ext-mbstring": "*" + }, "autoload": { "psr-4": { "Curl\\": "src/Curl/" diff --git a/src/Curl/StringUtil.php b/src/Curl/StringUtil.php index 39bf48c..e5ca0e9 100644 --- a/src/Curl/StringUtil.php +++ b/src/Curl/StringUtil.php @@ -4,6 +4,42 @@ namespace Curl; class StringUtil { + public static function characterReversePosition($haystack, $needle, $part = false) + { + if (function_exists('\mb_strrchr')) { + return \mb_strrchr($haystack, $needle, $part); + } else { + return \strrchr($haystack, $needle); + } + } + + public static function length($string) + { + if (function_exists('\mb_strlen')) { + return \mb_strlen($string); + } else { + return \strlen($string); + } + } + + public static function position($haystack, $needle, $offset = 0) + { + if (function_exists('\mb_strpos')) { + return \mb_strpos($haystack, $needle, $offset); + } else { + return \strpos($haystack, $needle, $offset); + } + } + + public static function reversePosition($haystack, $needle, $offset = 0) + { + if (function_exists('\mb_strrpos')) { + return \mb_strrpos($haystack, $needle, $offset); + } else { + return \strrpos($haystack, $needle, $offset); + } + } + /** * Return true when $haystack starts with $needle. * @@ -15,6 +51,15 @@ class StringUtil */ public static function startsWith($haystack, $needle) { - return \mb_substr($haystack, 0, \mb_strlen($needle)) === $needle; + return self::substring($haystack, 0, self::length($needle)) === $needle; + } + + public static function substring($string, $start, $length) + { + if (function_exists('\mb_substr')) { + return \mb_substr($string, $start, $length); + } else { + return \substr($string, $start, $length); + } } } diff --git a/src/Curl/Url.php b/src/Curl/Url.php index e31715e..9c8df63 100644 --- a/src/Curl/Url.php +++ b/src/Curl/Url.php @@ -56,10 +56,10 @@ class Url // buffer; otherwise, } elseif (StringUtil::startsWith($input, '/../')) { $input = substr($input, 3); - $output = substr_replace($output, '', \mb_strrpos($output, '/')); + $output = substr_replace($output, '', StringUtil::reversePosition($output, '/')); } elseif ($input === '/..') { $input = '/'; - $output = substr_replace($output, '', \mb_strrpos($output, '/')); + $output = substr_replace($output, '', StringUtil::reversePosition($output, '/')); // D. if the input buffer consists only of "." or "..", then remove // that from the input buffer; otherwise, @@ -70,7 +70,7 @@ class Url // the output buffer, including the initial "/" character (if // any) and any subsequent characters up to, but not including, // the next "/" character or the end of the input buffer. - } elseif (!(($pos = \mb_strpos($input, '/', 1)) === false)) { + } elseif (!(($pos = StringUtil::position($input, '/', 1)) === false)) { $output .= substr($input, 0, $pos); $input = substr_replace($input, '', 0, $pos); } else { @@ -131,7 +131,7 @@ class Url if (StringUtil::startsWith($r['path'], '/')) { $target['path'] = self::removeDotSegments($r['path']); } else { - $base = \mb_strrchr($b['path'], '/', true); + $base = StringUtil::characterReversePosition($b['path'], '/', true); if ($base === false) { $base = ''; }