PDA

View Full Version : Line break after X characters


psx-dude
Tue 2nd Dec '03, 2:08am
I know vb does this, but how would i go about inserting a < br > after X characters?

example: (bunch of e's, split for vb)
eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee eeeee

merk
Tue 2nd Dec '03, 3:58am
http://au.php.net/wordwrap

psx-dude
Tue 2nd Dec '03, 4:34am
http://au.php.net/wordwrap wordwrap doesn't cut up long words, found a comment snippet that works wonders:

function new_textwrap ($String, $breaksAt = 78, $breakStr = "\n", $cut = 1 , $padStr="") {
/*
new_textwrap 1.0 <linker@toter-link.de>

Based on textwrap 1.0 by Brian Moon <brian@phorum.org>
http://px.sklar.com/code-pretty.html?code_id=370

tested with PHP 3.0.16 WIN
You can use it as ' wordwrap ' from PHP4

$String The string to be wrapped.
$breaksAt How many characters each line should be.
$breakStr What character should be used to cause a break.
$cut If the cut is set to 1, the string is always wrapped at the specified width.
$padStr Allows for the wrapped lines to be padded at the begining.
*/

$newString="";
$lines=explode($breakStr, $String);
$cnt=count($lines);
for($x=0;$x<$cnt;$x++){
if(strlen($lines[$x])>$breaksAt){
$str=$lines[$x];
while(strlen($str)>$breaksAt){
$find = 1 ;
// echo $str."\n" ;
$pos=strrpos(substr($str, 0, $breaksAt+1), " ");
if ($pos == false) {
If($cut) {
$pos = $breaksAt ;
$find = 0 ;
} else {
$pos= strpos($str, " ");
if ($pos == false)
break;
}
}
$newString.=$padStr.substr($str, 0, $pos).$breakStr;
$str=(substr($str, $pos + $find)); // or $str=trim(substr($str, $pos));

}
$newString.=$padStr.$str.$breakStr;
}
else{
$newString.=$padStr.$lines[$x].$breakStr;
}
}

return substr ($newString,0, -(strlen($breakStr)));

} // end new_textwrap()

merk
Tue 2nd Dec '03, 5:39am
Example 2. wordwrap() example

<?php
$text = "A very long woooooooooooord.";
$newtext = wordwrap($text, 8, "\n", 1);
echo "$newtext\n";
?>

This example would display:

A very
long
wooooooo
ooooord.

:confused: ?

If the cut is set to 1, the string is always wrapped at the specified width. So if you have a word that is larger than the given width, it is broken apart. (See second example).

merk
Tue 2nd Dec '03, 5:42am
function new_textwrap ($String, $breaksAt = 78, $breakStr = "\n", $cut = 1 , $padStr="") {
/*
new_textwrap 1.0 <linker@toter-link.de>

Based on textwrap 1.0 by Brian Moon <brian@phorum.org>
http://px.sklar.com/code-pretty.html?code_id=370

tested with PHP 3.0.16 WIN
You can use it as ' wordwrap ' from PHP4

$String The string to be wrapped.
$breaksAt How many characters each line should be.
....]
It thinks my message is short. :(

psx-dude
Tue 2nd Dec '03, 2:18pm
Example 2. wordwrap() example

<?php
$text = "A very long woooooooooooord.";
$newtext = wordwrap($text, 8, "\n", 1);
echo "$newtext\n";
?>

This example would display:

A very
long
wooooooo
ooooord.

:confused: ?

If the cut is set to 1, the string is always wrapped at the specified width. So if you have a word that is larger than the given width, it is broken apart. (See second example).
you're right, when i tried it i forgot the last 1.

merk
Tue 2nd Dec '03, 5:29pm
No worries, i normally forget something :)