When passing to truncate_utf8() text that spans multiple lines, the function cuts the text on the first newline character if the $wordsafe flag is set to TRUE, no matter what $max_length is passed to it.

To test:

<?php
$text
= <<<EOD
This is a text that spans multiple lines.
Line 2 goes here.
EOD;
dsm(truncate_utf8($text, 51, TRUE));
dsm(truncate_utf8($text, 51, FALSE));
?>

Output:

  • This is a text that spans multiple lines.
  • This is a text that spans multiple lines.
    Line 2 go

Expected behavior:

  • This is a text that spans multiple lines.
    Line 2
  • This is a text that spans multiple lines.
    Line 2 go

The attached patch fixes this by adding the s (PCRE_DOTALL) modifier to the regular expression used.

Files: 
CommentFileSizeAuthor
#9 unicode-truncate-1712106-9-FAIL.patch904 bytesdawehner
FAILED: [[SimpleTest]]: [MySQL] 63,538 pass(es), 2 fail(s), and 0 exception(s).
[ View ]
#9 unicode-truncate-1712106-9.patch1.77 KBdawehner
PASSED: [[SimpleTest]]: [MySQL] 63,708 pass(es).
[ View ]
#8 drupal8-cut_on_the_first_line_wordsafe_true-1712106-8.patch913 bytesInternetDevels
PASSED: [[SimpleTest]]: [MySQL] 63,680 pass(es).
[ View ]

Comments

Attached patched works for me, thanks.

Version:7.x-dev» 8.x-dev
StatusFileSize
new814 bytes
PASSED: [[SimpleTest]]: [MySQL] 53,078 pass(es).
[ View ]

Re-rolling against 8.x-dev.

StatusFileSize
new913 bytes
PASSED: [[SimpleTest]]: [MySQL] 57,033 pass(es).
[ View ]

Re-roll to reflect core changes.

I just found this issue. Any idea when it will be fixed in the Core?

Thanks, attached patch works for me too. (7.x)

Issue summary:View changes
StatusFileSize
new794 bytes
FAILED: [[SimpleTest]]: [MySQL] Unable to apply patch truncate_utf8-wordsafe-newlines-1712106-6.patch. Unable to apply patch. See the log in the details link for more information.
[ View ]

Attached is an updated patch against 7.26 (with offset changes only).

Status:Needs review» Needs work

The last submitted patch, 6: truncate_utf8-wordsafe-newlines-1712106-6.patch, failed testing.

Status:Needs work» Needs review
StatusFileSize
new913 bytes
PASSED: [[SimpleTest]]: [MySQL] 63,680 pass(es).
[ View ]

Interdiff is empty, because function truncate_utf8 moved to Unicode::truncate.

Status:Needs review» Reviewed & tested by the community
StatusFileSize
new1.77 KB
PASSED: [[SimpleTest]]: [MySQL] 63,708 pass(es).
[ View ]
new904 bytes
FAILED: [[SimpleTest]]: [MySQL] 63,538 pass(es), 2 fail(s), and 0 exception(s).
[ View ]

The code looked fine, though there was clearly no test so I went ahead and used the issue summary to create one

Status:Reviewed & tested by the community» Needs review

ups

Status:Needs review» Needs work

The last submitted patch, 9: unicode-truncate-1712106-9-FAIL.patch, failed testing.