php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #74617 wordwrap ignores newlines
Submitted: 2017-05-18 22:19 UTC Modified: 2017-05-22 16:01 UTC
Votes:1
Avg. Score:5.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:0 (0.0%)
Same OS:0 (0.0%)
From: bob dot terrell at acordex dot com Assigned:
Status: Open Package: Strings related
PHP Version: 5.6.30 OS: Any
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: bob dot terrell at acordex dot com
New email:
PHP Version: OS:

 

 [2017-05-18 22:19 UTC] bob dot terrell at acordex dot com
Description:
------------
wordwrap ignores newline characters when deciding how to wrap a string.

Consider the following string:

$str = "123456789 123456789\n123 123 123";

Using wordwrap($str, 25, "\n"), you would expect two lines, as you want to wrap each line to 25 characters.

You actually get three, because it ignores the fact that a newline exists at the 20th character, and it wraps the string after the first "123" word.

Test script:
---------------
<?php

$str = "123456789 123456789\n123 123 123";

echo wordwrap($str, 25, "\n");

?>

Expected result:
----------------
Expected output:

123456789 123456789
123 123 123

Actual result:
--------------
Actual output:
123456789 123456789
123
123 123

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2017-05-21 11:46 UTC] cmb@php.net
-Status: Open +Status: Feedback -Assigned To: +Assigned To: cmb
 [2017-05-21 11:46 UTC] cmb@php.net
I can't reproduce the described behavior, see
<https://3v4l.org/Nm8nG>. Please provide details about the PHP
version you are using.
 [2017-05-22 15:12 UTC] bob dot terrell at acordex dot com
Sorry, I think I missed a character in the test script. Try this:

<?php

$str = "123456789 123456789\n123 123 123";

echo wordwrap($str, 25, "\n ");

?>

Note the space after the newline in the replace string.

What I'm trying to do is word wrap preformatted text and indent the line by one space if it's forced to wrap.

I had expected that it would see the newline in the middle of the string and start the counter over from there. Is it perhaps treating the break parameter as the only string that should be considered a newline and ignoring the \n in the string?

Perhaps this is a documentation problem. The documentation says "The line is broken using the optional break parameter." I took this to mean that if a line must be broken, that's the string to insert. Is it also the string that's searched for? If so, that isn't clear.
 [2017-05-22 16:01 UTC] cmb@php.net
-Status: Feedback +Status: Open -Type: Bug +Type: Documentation Problem -Assigned To: cmb +Assigned To:
 [2017-05-22 16:01 UTC] cmb@php.net
> Is it perhaps treating the break parameter as the only string
> that should be considered a newline and ignoring the \n in the
> string?

That appears to be so, see <https://3v4l.org/72PPC>.
 [2017-05-22 17:43 UTC] bob dot terrell at acordex dot com
I'll leave a comment on the wordwrap page, but it might be a good idea to edit the documentation.

I guess this can either be set to "not a bug" or "documentation problem."
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Dec 21 16:01:28 2024 UTC