Fix for wordwrap()
| From: | Jedi/Sector One | Date: | Thu, 06 Mar 2003 11:52:47 +0000 |
| Subject: | Fix for wordwrap() | ||
| Groups: | php.dev | ||
| Request: | Send a blank email to php-dev+get-94833@lists.php.net to get a copy of this message | ||
Hello.
Maybe this is the intended behavior, but wordwrap()'s behavior is a bit
illogical on PHP 4.3.1, as it does only break after a plain whitespace, not
after punctuation, \n, etc.
Here's a trivial sample :
$a = "wwwwwwwwwwwwwwwwww\nphprules\nwwwwwwwwwwwwwwwww";
print wordwrap($a, 10, ' ', 10);
Output :
wwwwwwwwww wwwwwwww
p hprules
ww wwwwwwwwww wwwww
The following trivial patch makes wordwrap() wrap after any
non-alphanumeric charater. Output after application of that patch :
wwwwwwwwww wwwwwwww phprules wwwwwwwwww wwwwwww
Best regards,
-Frank.
--- php-4.3.1/ext/standard/string.c 2002-12-27 04:22:20.000000000 +0100 +++ php-4.3.1-jedi/ext/standard/string.c 2003-03-06 12:41:46.000000000 +0100 @@ -654,7 +654,7 @@ for (current = 0; current < textlen; current++) { if (text[current] == breakchar[0]) { laststart = lastspace = current; - } else if (text[current] == ' ') { + } else if (!isalnum((unsigned char) text[current])) { if (current - laststart >= linelength) { newtext[current] = breakchar[0]; laststart = current; @@ -701,7 +701,7 @@ } /* if it is a space, check if it is at the line boundary, * copy and insert a break, or just keep track of it */ - else if (text[current] == ' ') { + else if (!isalnum((unsigned char) text[current])) { if (current - laststart >= linelength) { memcpy(newtext+newtextlen, text+laststart, current-laststart); newtextlen += current - laststart;
--- php-4.3.1/ext/standard/string.c 2002-12-27 04:22:20.000000000 +0100 +++ php-4.3.1-jedi/ext/standard/string.c 2003-03-06 12:41:46.000000000 +0100 @@ -654,7 +654,7 @@ for (current = 0; current < textlen; current++) { if (text[current] == breakchar[0]) { laststart = lastspace = current; - } else if (text[current] == ' ') { + } else if (!isalnum((unsigned char) text[current])) { if (current - laststart >= linelength) { newtext[current] = breakchar[0]; laststart = current; @@ -701,7 +701,7 @@ } /* if it is a space, check if it is at the line boundary, * copy and insert a break, or just keep track of it */ - else if (text[current] == ' ') { + else if (!isalnum((unsigned char) text[current])) { if (current - laststart >= linelength) { memcpy(newtext+newtextlen, text+laststart, current-laststart); newtextlen += current - laststart;