PDA

View Full Version : [PHP] Wordrap - just long words



DaveMania
March 31st, 2005, 06:44 PM
Is there a way of getting the wordwrap() function to just wrap the idividual words (un separated by spaces) and not sentences that have spaces in them.

I.E.

Turning
'This is a very long woooooooooooooooooooooord'

Into
'This is a very long
woooooooooooooo
oooooooord'

Not
'This is a very
long wooooooooo
ooooooooooooord'

(the examples split every 15 chars)

Any help would be appreciated
Thanks

spartan018
March 31st, 2005, 06:52 PM
$max_langth = 15;
$separate_words = explode(" ", $string);

for($i = 0; $i < count($separate_words); $i++)
{
if( strlen($separate_words[$i]) > $max_length )
{
$separate_words[$i] = wordwrap($separate_words[$i], $max_length,"<br>\n");
}
}

$string = implode(" ", $separate_words);

that should do the trick. might be helpful to make it a function too

DaveMania
March 31st, 2005, 07:21 PM
sorry but what's the input and what's the output? :D My setup uses

$message = $_POST["message"];

It gets it then writes it to a file - how could i sdjust this to the code u gave me/adjust the code to fit my setup?

thanks

spartan018
March 31st, 2005, 08:15 PM
function customWrap($string, $max_length)
{
$max_langth = 15;
$separate_words = explode(" ", $string);

for($i = 0; $i < count($separate_words); $i++)
{
if( strlen($separate_words[$i]) > $max_length )
{
$separate_words[$i] = wordwrap($separate_words[$i], $max_length,"<br>\n");
}
}

$string = implode(" ", $separate_words);
return $string;
}

$message = $_POST[message];
print customWrap($message, 20); //20 is max length of a word


this code actually isnt very useful as i just realized.

example:
input = "These are loooooooooooonnnnnnnnnggggg wooooooorrrrrrdddddssss"
output = "These are loooooooooooonnnnnnn
nnggggg wooooooorrrrrrddddds
sss"

if there are two long words next to each other, than u have two short lines. there is a much better way than mine, but i dont know it