note 73756 added to function.strlen
| From: | Petr1fied at osu1 dot php dot net | Date: | Fri, 09 Mar 2007 00:28:46 +0000 |
| Subject: | note 73756 added to function.strlen | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-124254@lists.php.net to get a copy of this message | ||
I was looking for a function to add line breaks if the string was over a specified number of
characters in length, I couldn't find one so I made my own. It's easy enough to use, just
pass the string and the maximum length for the line to the function and it should return a string
with line breaks.
This is my first function so go easy on me. :D
<?php
$teststring="This is a very long string and I want to have line breaks with no more than 50
characters per line so I'm going to call my new function and see what happens. Hopefully it
should work as anticipated! ;)";
$test=strlnbrk($teststring, 50);
echo $test;
/*
Output should be:
This is a very long string and I want to have
line breaks with no more than 50 characters per
line so I'm going to call my new function and see
what happens. Hopefully it should work as
anticipated! ;)
*/
function strlnbrk($string, $length) {
$i=0;
$line=array();
$data=explode(" ", $string);
foreach($data as $word) {
if(strlen($line[$i])+strlen(" ".$word)>$length) {
$i++;
$line[$i].=" ".$word;
} else
$line[$i].=" ".$word;
}
$output="";
foreach($line as $finalise)
$output.=$finalise."<br />";
$output=substr($output, 0, strlen($output)-6);
return $output;
}
?>
----
Server IP: 216.194.113.175
Probable Submitter: 172.159.96.4
----
Manual Page -- http://www.php.net/manual/en/function.strlen.php
Edit -- https://master.php.net/note/edit/73756
Del: integrated -- https://master.php.net/note/delete/73756/integrated
Del: useless -- https://master.php.net/note/delete/73756/useless
Del: bad code -- https://master.php.net/note/delete/73756/bad+code
Del: spam -- https://master.php.net/note/delete/73756/spam
Del: non-english -- https://master.php.net/note/delete/73756/non-english
Del: in docs -- https://master.php.net/note/delete/73756/in+docs
Del: other reasons-- https://master.php.net/note/delete/73756
Reject -- https://master.php.net/note/reject/73756
Search -- https://master.php.net/manage/user-notes.php