note 73756 deleted from function.strlen by philip
| From: | philip@php.net | Date: | Fri, 09 Mar 2007 03:03:03 +0000 |
| Subject: | note 73756 deleted from function.strlen by philip | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-124255@lists.php.net to get a copy of this message | ||
Note Submitter: Petr1fied
----
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;
}
?>