Jump to content

[SOLVED] WordWrap, Not quit it...


shlomikalfa

Recommended Posts

This page: http://www.e-lephant.org/Download.php?ID=694

 

in there you can see the problem, the comments are too long...

as i have tried this:

$Text = wordwrap($Text, 50, "<br />\n", true);

 

it is not quit what i was looking for... i wanted it to check if the string is without any space for that long then cut if... else, leave it as is....

 

Any built-in function that does that?!

 

Or should i take the string, split it by spaces, check the length of each and every chunk and then return it ?!

Link to comment
https://forums.phpfreaks.com/topic/131204-solved-wordwrap-not-quit-it/
Share on other sites

why doesn't this work:

function DynWarpText($Text, $HTML1stWrap = -1, $SubWrap = -1){
// Splits the string by spaces ' ', then checks each entry for length.
//[$HTML1stWrap:: Instead of trimming it use HTMLwrap and add: '" "']
//[$SubWrap:: Secondaty Wraping size to use after text reaches first size!]

$TextArr = explode(" ", $Text);
$retText = "";
if ($SubWrap == -1){$SubWrap = $HTML1stWrap;}
for ($i = 0; $i <= count($TextArr); $i++){
// Just check the size and split if needed!
	if (strlen($TextArr[$i]) > $HTML1stWrap){
		// If we've reached the size for the first line, we use 2nd line.
		if (strlen($retText) > $HTML1stWrap){
			$retText .= wordwrap($TextArr[$i], $SubWrap, "- ", true)." ";
		}else {
			$retText .= wordwrap($TextArr[$i], $HTML1stWrap, " ", true)." ";
		}
	}else {
		$retText .= $TextArr[$i]." ";
	}
}
return $retText;
}

 

It never switches to the '$SubWrap' Word Wrapping....

Solved....

 

For next to come:

function DynWarpText($Text, $HTML1stWrap = -1, $SubWrap = -1){
// Splits the string by spaces ' ', then checks each entry for length.
//[$HTML1stWrap:: Instead of trimming it use HTMLwrap and add: '" "']
//[$SubWrap:: Secondaty Wraping size to use after text reaches first size!]

$TextArr = explode(" ", $Text);
$retText = "";

for ($i = 0; $i <= count($TextArr); $i++){
// Just check the size and split if needed!
	if (strlen($TextArr[$i]) > $HTML1stWrap){
		if ($SubWrap > -1){
			$retText .= substr($TextArr[$i], 0, $HTML1stWrap)." ";
			$TextArr[$i] = substr($TextArr[$i], $HTML1stWrap+1);
			$retText .= wordwrap($TextArr[$i], $SubWrap, "- ", true)." ";
		}else {
			$retText .= wordwrap($TextArr[$i], $HTML1stWrap, " ", true)." ";
		}
	}else {
		$retText .= $TextArr[$i]." ";
	}
}
return $retText;
}

 

Use the above function to form your code if you're using tables and you don't want your tables to go BANANAz...

P.S -> Combine with monospaced fonts to make sure your sizes are correct: [#CSS:]

#CommentsTd{font: 14px Courier New}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.