Re: note 78044 rejected and deleted from function.wordwrap by notes editor didou
| From: | Ian O'Shaughnessy | Date: | Mon, 08 Oct 2007 14:50:34 +0000 |
| Subject: | Re: note 78044 rejected and deleted from function.wordwrap by notes editor didou | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-131235@lists.php.net to get a copy of this message | ||
Why was my comment deleted? I posted a MB-String version of wordwrap,
that so far has not been posted by anyone else..
I just looked at php.net/wordwrap and my comment seems to have been
replaced by golanzakaiATpatternDOTcoDOTil who I'm guessing is a
PHP.net editor since my comment was rejected at the same time as this
email was sent.
However, the function that he posted, is not a perfect replacement of
wordwrap() as it lacks the last parameter "$cut". That parameter is an
important part of the function. Replacing the code that I posted with
inferior code does a disservice to the community, even if the editors
of php think they write cleaner or faster code..
On 10/8/07, php-notes@lists.php.net <php-notes@lists.php.net> wrote:
> You are receiving this email because your note posted
> to the online PHP manual has been removed by one of the editors.
>
> Read the following paragraphs carefully, because they contain
> pointers to resources better suited for requesting support or
> reporting bugs, none of which are to be included in manual notes
> because there are mechanisms and groups in place to deal with
> those issues.
>
> The user contributed notes are not an appropriate place to
> ask questions, report bugs or suggest new features; please
> use the resources listed on <http://php.net/support>
> for those purposes. This was clearly stated in the page
> you used to submit your note, please carefully re-read
> those instructions before submitting future contributions.
>
> Bug submissions and feature requests should be entered at
> <http://bugs.php.net/>. For documentation errors use the
> bug system, and classify the bug as "Documentation problem".
> Support and ways to find answers to your questions can be found
> at <http://php.net/support>.
>
> Your note has been removed from the online manual.
>
> ----- Copy of your note below -----
>
> wordwrap() doesn't handle Unicode (MB) strings properly, so I wrote a new mb_wordwrap()
> that handles them properly:
>
> function mb_wordwrap($str, $width = 70, $break = "\n", $cut = false)
> {
> $return = '';
> $str_bytes = strlen($str);
> $first_char = true;
>
> $current_line = '';
> $current_line_char_count = 0;
> $current_word = '';
> $current_word_char_count = 0;
>
> for ($i=0; $i < $str_bytes; $i++)
> {
> //get the next char (unicode or ascii)
> $char = $str{$i};
> $h = ord($char);
> if ($h <= 0x7F)
> { $char_code = $h; }
> else if ($h < 0xC2)
> { $char_code = false; }
> else if ($h <= 0xDF)
> {
> $c2 = $str{++$i};
> $char .= $c2;
> $char_code = ($h & 0x1F) << 6 | (ord($c2) & 0x3F);
> }
> else if ($h <= 0xEF)
> {
> $c2 = $str{++$i};
> $c3 = $str{++$i};
> $char .= $c2.$c3;
> $char_code = ($h & 0x0F) << 12 | (ord($c2) & 0x3F)
> << 6 | (ord($c3) & 0x3F);
> }
> else if ($h <= 0xF4)
> {
> $c2 = $str{++$i};
> $c3 = $str{++$i};
> $c4 = $str{++$i};
> $char .= $c2.$c3.$c4;
> $char_code = ($h & 0x0F) << 18 | (ord($c2) & 0x3F)
> << 12 | (ord($c3) & 0x3F) << 6 | (ord($c4) & 0x3F);
> }
> else
> {
> //unrecognized char, skip it
> continue;
> }
>
> //if it's a space, new word commencing
> if ($char_code == 32)
> {
> //if line is too long, linebreak time!
> if ($current_line_char_count + $current_word_char_count >= $width)
> {
> if ($current_line_char_count)
> { $return .= $current_line.$break; }
>
> //reset the current line
> $current_line = $current_word;
> $current_line_char_count = $current_word_char_count;
> }
> else
> {
> //include a space at the front of the word if this isn't
> the first char
> //since we assume there was a space prior to this word except
> for the first word
> $current_line .= ($first_char ? '' : '
> ').$current_word;
> $current_line_char_count += $current_word_char_count +
> ($first_char ? 0 : 1);
> }
>
> $current_word = '';
> $current_word_char_count = 0;
>
> $first_char = false;
> }
> //if it's a char, add it to the word
> else
> {
> if ($cut)
> {
> //check if this word is too long. if it is, slice it.
> if ($current_word_char_count >= $width)
> {
> //clear the current line and word to the return value
> if ($current_line_char_count)
> { $return .= $current_line.$break; }
>
> $current_line = $current_word;
> $current_line_char_count = $current_word_char_count;
>
> $current_word = '';
> $current_word_char_count = 0;
> }
> }
>
> $current_word .= $char;
> $current_word_char_count++;
> }
> }
>
> //check for leftovers and add them to the string
> if ($current_word_char_count)
> { $return .= $current_line.($current_word_char_count ? ($current_word_char_count +
> $current_line_char_count > $width ? "\n" : ' ').$current_word :
> ''); }
>
> return $return;
> }
>
--
DJneoform A.K.A. Ian
www.djneoform.com