Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[5.2] Fix multibyte string functions #12953

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 13 additions & 13 deletions src/Illuminate/Support/Str.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public static function camel($value)
public static function contains($haystack, $needles)
{
foreach ((array) $needles as $needle) {
if ($needle != '' && strpos($haystack, $needle) !== false) {
if ($needle != '' && mb_strpos($haystack, $needle) !== false) {
return true;
}
}
Expand All @@ -84,7 +84,7 @@ public static function contains($haystack, $needles)
public static function endsWith($haystack, $needles)
{
foreach ((array) $needles as $needle) {
if ((string) $needle === substr($haystack, -strlen($needle))) {
if ((string) $needle === static::substr($haystack, -static::length($needle))) {
return true;
}
}
Expand All @@ -103,7 +103,7 @@ public static function finish($value, $cap)
{
$quoted = preg_quote($cap, '/');

return preg_replace('/(?:'.$quoted.')+$/', '', $value).$cap;
return preg_replace('/(?:'.$quoted.')+$/u', '', $value).$cap;
}

/**
Expand All @@ -126,7 +126,7 @@ public static function is($pattern, $value)
// pattern such as "library/*", making any string check convenient.
$pattern = str_replace('\*', '.*', $pattern).'\z';

return (bool) preg_match('#^'.$pattern.'#', $value);
return (bool) preg_match('#^'.$pattern.'#u', $value);
}

/**
Expand Down Expand Up @@ -180,7 +180,7 @@ public static function words($value, $words = 100, $end = '...')
{
preg_match('/^\s*+(?:\S++\s*+){1,'.$words.'}/u', $value, $matches);

if (! isset($matches[0]) || strlen($value) === strlen($matches[0])) {
if (! isset($matches[0]) || static::length($value) === static::length($matches[0])) {
return $value;
}

Expand Down Expand Up @@ -221,12 +221,12 @@ public static function random($length = 16)
{
$string = '';

while (($len = strlen($string)) < $length) {
while (($len = static::length($string)) < $length) {
$size = $length - $len;

$bytes = static::randomBytes($size);

$string .= substr(str_replace(['/', '+', '='], '', base64_encode($bytes)), 0, $size);
$string .= static::substr(str_replace(['/', '+', '='], '', base64_encode($bytes)), 0, $size);
}

return $string;
Expand Down Expand Up @@ -255,7 +255,7 @@ public static function quickRandom($length = 16)
{
$pool = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';

return substr(str_shuffle(str_repeat($pool, $length)), 0, $length);
return static::substr(str_shuffle(str_repeat($pool, $length)), 0, $length);
}

/**
Expand Down Expand Up @@ -283,9 +283,9 @@ public static function equals($knownString, $userInput)
return hash_equals($knownString, $userInput);
}

$knownLength = mb_strlen($knownString, '8bit');
$knownLength = static::length($knownString, '8bit');

if (mb_strlen($userInput, '8bit') !== $knownLength) {
if (static::length($userInput, '8bit') !== $knownLength) {
return false;
}

Expand Down Expand Up @@ -372,9 +372,9 @@ public static function snake($value, $delimiter = '_')
}

if (! ctype_lower($value)) {
$value = preg_replace('/\s+/', '', $value);
$value = preg_replace('/\s+/u', '', $value);

$value = static::lower(preg_replace('/(.)(?=[A-Z])/', '$1'.$delimiter, $value));
$value = static::lower(preg_replace('/(.)(?=[A-Z])/u', '$1'.$delimiter, $value));
}

return static::$snakeCache[$key] = $value;
Expand All @@ -390,7 +390,7 @@ public static function snake($value, $delimiter = '_')
public static function startsWith($haystack, $needles)
{
foreach ((array) $needles as $needle) {
if ($needle != '' && strpos($haystack, $needle) === 0) {
if ($needle != '' && mb_strpos($haystack, $needle) === 0) {
return true;
}
}
Expand Down