Skip to content
Merged
Show file tree
Hide file tree
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
17 changes: 14 additions & 3 deletions src/Php74/Php74.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,17 +50,28 @@ public static function mb_str_split($string, $split_length = 1, $encoding = null
}

if (1 > $split_length = (int) $split_length) {
trigger_error('The length of each segment must be greater than zero', \E_USER_WARNING);
if (80000 > \PHP_VERSION_ID) {
trigger_error('The length of each segment must be greater than zero', \E_USER_WARNING);

return false;
return false;
}

throw new \ValueError('Argument #2 ($length) must be greater than 0');
}

if (null === $encoding) {
$encoding = mb_internal_encoding();
}

if ('UTF-8' === $encoding || \in_array(strtoupper($encoding), ['UTF-8', 'UTF8'], true)) {
return preg_split("/(.{{$split_length}})/us", $string, -1, \PREG_SPLIT_DELIM_CAPTURE | \PREG_SPLIT_NO_EMPTY);
$rx = '/(';
while (65535 < $split_length) {
$rx .= '.{65535}';
$split_length -= 65535;
}
$rx .= '.{'.$split_length.'})/us';

return preg_split($rx, $string, -1, \PREG_SPLIT_DELIM_CAPTURE | \PREG_SPLIT_NO_EMPTY);
}

$result = [];
Expand Down
22 changes: 22 additions & 0 deletions tests/Php74/Php74Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Symfony\Polyfill\Tests\Php74;

use PHPUnit\Framework\TestCase;
use Symfony\Polyfill\Php74\Php74;

/**
* @author Ion Bazan <ion.bazan@gmail.com>
Expand Down Expand Up @@ -116,6 +117,27 @@ public function testStrSplitWithInvalidValues()
$this->expectWarningMessage('The length of each segment must be greater than zero');
mb_str_split('победа', 0);
}

/**
* @covers \Symfony\Polyfill\Php74\Php74::mb_str_split
*
* @requires PHP 8
*/
public function testStrSplitThrowsOnInvalidLength()
{
$this->expectException(\ValueError::class);
$this->expectExceptionMessage('Argument #2 ($length) must be greater than 0');

Php74::mb_str_split('победа', 0);
}

/**
* @covers \Symfony\Polyfill\Php74\Php74::mb_str_split
*/
public function testStrSplitWithLengthAbovePcreLimit()
{
$this->assertSame([str_repeat('x', 70000)], Php74::mb_str_split(str_repeat('x', 70000), 70000, 'UTF-8'));
}
}

class A
Expand Down