Skip to content

Instantly share code, notes, and snippets.

@renan
Created September 30, 2013 12:39
Show Gist options
  • Save renan/6763224 to your computer and use it in GitHub Desktop.
Save renan/6763224 to your computer and use it in GitHub Desktop.
diff --git a/lib/Cake/Utility/String.php b/lib/Cake/Utility/String.php
index f6d9bf4..aea9421 100644
--- a/lib/Cake/Utility/String.php
+++ b/lib/Cake/Utility/String.php
@@ -358,25 +358,20 @@ class String {
* @return string Formatted text.
*/
public static function wordWrap($text, $width = 72, $break = "\n", $cut = false) {
- $widthAndBoundary = $cut ? '{' . $width . '}#' : '{' . $width . ',}\b#U';
- $regexp = '#^(?:[\x00-\x7F]|[\xC0-\xFF][\x80-\xBF]+)' . $widthAndBoundary;
-
- $length = mb_strlen($text);
- $whileWhat = ceil($length / $width);
- $i = 1;
- $return = '';
- while ($i < $whileWhat) {
- preg_match($regexp, $text, $matches);
- if (!$matches) {
- $i++;
- continue;
+ $parts = array();
+ while (mb_strlen($text) > 0) {
+ if ($cut) {
+ $part = mb_substr($text, 0, $width);
+ } else {
+ preg_match('/^(.{0,' . $width . '})[\s\.\,]/u', $text, $matches);
+ $part = $matches[0];
}
- $string = $matches[0];
- $return .= trim($string) . $break;
- $text = trim(mb_substr($text, mb_strlen($string)));
- $i++;
+
+ $parts[] = trim($part);
+ $text = trim(mb_substr($text, mb_strlen($part)));
}
- return $return . $text;
+
+ return implode($break, $parts);
}
/**
public static function wordWrap($text, $width = 72, $break = "\n", $cut = false) {
$parts = array();
while (mb_strlen($text) > 0) {
if ($cut) {
$part = mb_substr($text, 0, $width);
} else {
preg_match('/^(.{0,' . $width . '})[\s\.\,]/u', $text, $matches);
$part = $matches[0];
}
$parts[] = trim($part);
$text = trim(mb_substr($text, mb_strlen($part)));
}
return implode($break, $parts);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment