note 107193 added to function.wordwrap
| From: | holdoffhunger at gmail dot com | Date: | Fri, 13 Jan 2012 16:16:38 +0000 |
| Subject: | note 107193 added to function.wordwrap | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-185633@lists.php.net to get a copy of this message | ||
I wish I could wordwrap only long words, ignore the rest, and not wordwrap any html. And with this
function, I can! (It's built to ignore html/xml tags that it runs into.)
<?php
// Author: holdoffhunger@gmail.com
// License: AGPL3
$text_to_wordwrap = "<p
class=\"some-really-long-class-name-that's-long\">There's one
loooooooooooooooooong word in this string.</p>"
$wordwrapped_text = $text_to_wordwrap;
$max_word_length = 15;
$wordwrap_every_x_chars = 2;
$delimiter = "<wbr>";
$length_of_text_to_wordwrap = strlen($wordwrapped_text);
$inside_an_xml_tag = 0;
$current_non_tag_word_size = 0;
$number_of_tag_chars = 0;
$number_of_regular_chars = 0;
$added_delimiters = 0;
for($i = 0; $i < $length_of_text_to_wordwrap; $i++)
{
$current_char_to_examine = $wordwrapped_text[$i];
if($current_char_to_examine == "<")
{
$inside_an_xml_tag = 1;
}
if($inside_an_xml_tag == 1)
{
$number_of_tag_chars++;
$current_non_tag_word_size = 0;
}
if($inside_an_xml_tag == 0)
{
$number_of_regular_chars++;
}
if( ($current_char_to_examine == " ") ||
($current_char_to_examine == "\t") ||
($current_char_to_examine == "\n") ||
($current_char_to_examine == "\r") ||
($current_char_to_examine == "\s") ||
($current_char_to_examine == "\"") ||
($current_char_to_examine == "(") ||
($current_char_to_examine == ")") ||
($current_char_to_examine == "[") ||
($current_char_to_examine == "]") ||
($current_char_to_examine == "{") ||
($current_char_to_examine == "}") )
{
$current_non_tag_word_size = 0;
}
else
{
$current_non_tag_word_size++;
}
if( ($current_non_tag_word_size > $max_word_length) &&
($inside_an_xml_tag == 0) )
{
$left_side = substr($wordwrapped_text, 0, $i - $current_non_tag_word_size + 1);
$previous_word_length = $current_non_tag_word_size;
$word_so_far = substr($wordwrapped_text, $i - $previous_word_length + 1,
$current_non_tag_word_size);
for($i_search = $i + 1; $i_search < $length_of_text_to_wordwrap; $i_search++)
{
$next_char = $wordwrapped_text[$i_search];
if( ($next_char != " ") &&
($next_char != "\t") &&
($next_char != "\n") &&
($next_char != "\r") &&
($next_char != "\s") &&
($next_char != "\"") &&
($next_char != "(") &&
($next_char != ")") &&
($next_char != "[") &&
($next_char != "]") &&
($next_char != "{") &&
($next_char != "}") )
{
$current_non_tag_word_size++;
}
else
{
$i_search = $length_of_text_to_wordwrap;
}
}
$entire_word = substr($wordwrapped_text, $i - $previous_word_length + 1,
$current_non_tag_word_size);
$length_of_entire_word = strlen($entire_word);
$right_side = substr($wordwrapped_text, $i - $previous_word_length + 1 + $length_of_entire_word,
$length_of_text_to_wordwrap);
$new_word = wordwrap($entire_word, $wordwrap_every_x_chars, $delimiter, TRUE);
$test = str_replace($delimiter, $delimiter, $new_word, $number_of_delimiters);
$added_delimiters += $number_of_delimiters;
$new_entry = $left_side . $new_word . $right_side;
$new_entry_so_far_test = $left_side . $new_word;
$i = strlen($new_entry_so_far_test) - 1;
$wordwrapped_text = $new_entry;
}
if($current_char_to_examine == ">")
{
$inside_an_xml_tag = 0;
$current_non_tag_word_size = 0;
}
}
// Result:
// <p class="some-really-long-class-name-that's-long">There's one
lo<wbr>oo<wbr>oo<wbr>oo<wbr>oo<wbr>oo<wbr>oo<wbr>oo<wbr>oo<wbr>on<wbr>g
word in this string.</p>
?>
----
Server IP: 69.147.83.197
Probable Submitter: 68.9.137.178
----
Manual Page -- http://www.php.net/manual/en/function.wordwrap.php
Edit -- https://master.php.net/note/edit/107193
Del: integrated -- https://master.php.net/note/delete/107193/integrated
Del: useless -- https://master.php.net/note/delete/107193/useless
Del: bad code -- https://master.php.net/note/delete/107193/bad+code
Del: spam -- https://master.php.net/note/delete/107193/spam
Del: non-english -- https://master.php.net/note/delete/107193/non-english
Del: in docs -- https://master.php.net/note/delete/107193/in+docs
Del: other reasons-- https://master.php.net/note/delete/107193
Reject -- https://master.php.net/note/reject/107193
Search -- https://master.php.net/manage/user-notes.php