Wrapping text in web pages

Wrapping text in web pages

We can use the following function to wrap the text in web pages. While calling function we can give input text,limit and divider [optional]. This function breaks the line after the limit is reached.

<?php

function text_wrap($log_text, $limit, $divider=” “) {

$words = explode($divider, $log_text);

$word_count = count($words);

$char_counter = 0;

$block = “”;

foreach ($words as $value) {

$chars = strlen($value);

$block .= $value;

$char_counter = $char_counter + $chars;

if ($char_counter >= $limit) {

$block .= ”
“;

$char_counter = 0;

} else {

$block .= ” “;

}

}

return rtrim($block);

}

$text=”This is test lonnnnnnnggggggg string”;

$limit=5;

echo $output_text=text_wrap($text,$limit);

?>

Leave a Reply

You must be logged in to post a comment.


All material @ copyrighted by chrisranjana.com. If you want to link to this article you are welcome to do so. Unauthorized publication is strictly prohibited. This developer tutorial website contains articles by Php programmers , Software developers, Mysql programmers and asp c# programmers. This website also contains ajax tutorials and advanced mysql sql stored procedures and functions tutorials and sample codes.