Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

mb_str_split() bug when string contains newline #211

Copy link
Copy link
@njandreasson

Description

@njandreasson
Issue body actions

I stumbled upon a bug where mb_str_split() returned an odd response. After some research I found out it happened when input string has one or more newlines.

Example code to reproduce:

$str = "Hello!\nThis is an example with a string with new line.\nI'm just making an example to point out how mb_str_split() was broken in 1.13.0 when string has new lines. Thanks for reading this long example string! Have a nice day!";

var_dump(mb_strlen($str));
var_dump(mb_str_split($str, 153));

Expected output (https://3v4l.org/L6M1K):

int(223)
array(2) {
  [0]=>
  string(153) "Hello!
This is an example with a string with new line.
I'm just making an example to point out how mb_str_split() was broken in 1.13.0 when string has ne"
  [1]=>
  string(70) "w lines. Thanks for reading this long example string! Have a nice day!"
}

The problem started in 1.13.0 where #199 was merged.
If I understand the intention of the pull request correctly he wanted to return early when split length is 1, his initial example was:

if (1 === $split_length && 'UTF-8' === $encoding) {
    return preg_split('//u', $string, null, PREG_SPLIT_NO_EMPTY);
}

Actual output with polyfill 1.13.0+:

int(223)
array(3) {
  [0]=>
  string(55) "Hello!
This is an example with a string with new line.
"
  [1]=>
  string(153) "I'm just making an example to point out how mb_str_split() was broken in 1.13.0 when string has new lines. Thanks for reading this long example string! H"
  [2]=>
  string(15) "ave a nice day!"
}

The problem seems to be that in the final commit you are not checking if $split_length is actually 1, which results in that preg_split() is always used:

8dd88d7#diff-e8706b247d3e17dac17a413b65b64193R547

Reactions are currently unavailable

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      Morty Proxy This is a proxified and sanitized view of the page, visit original site.