Hi,

Im currently using fpdf to generate an auto report, but im having an issue with overflow on the notes column, as this can be anything from 1 to 300 words.

i can count the words using the following script, but im struggling to find out how to limit the number of words per line (so no overflow).

function adv_count_words($str) 
{
$words = 0;
$str = eregi_replace(" +", " ", $str);
$array = explode(" ", $str);
for($i=0;$i < count($array);$i++)
{
if (eregi("[0-9A-Za-zÀ-ÖØ-öø-ÿ]", $array[$i])) 
$words++;
}
return $words;
}
// this next like is used to output into the pdf
$pdf->Cell(0,10,$words,0,1);

This might be wrong, I have never really used fpdf but...

Words have different lengths, so, for example a line with 5 words may only have 18 characters, it may also have 31, so surely you would have to use a character count as well?

hi,

cheers for the response. At the moment im using strlen and limiting it to, i think 100 characters. This is fine but its cutting the final word short like th
is

Well, that's a good exa
mple ;)

You could try mixing in your adv_count_words($str) function or something. You would need to find a way of making sure the last character is a space or the 101th character is a space, otherwise find the last space and split it there, if that makes sense.

Kieran :)

i think ill try exploding it into an array and spitting it out. Ill probably have to shorted the lines a little bit but at least the words will be whole

im here so far. not there yet but i think i can smell it

<?php
ini_set('display_errors',1);
error_reporting(E_ALL|E_STRICT);
  $values = array(3,3,3,3,3,3,1,1,1,1,2);
   $stateFrequency = array_count_values($values);
   print_r($stateFrequency);
   echo "<br>".$stateFrequency[1];
echo "<br><br>";
$str = "Hello world. It's a beautiful day. lots of words and stuff and things and all sorts of shite.  yada yada yada yada yada yada yada yada yada yada yada yada yada yada yada";
$str2 = (explode(" ",$str));
print count($str2);
$count = 0;
for ($count==0;$count == count($str2);$count++){
		echo $str2[$count] . "";
		if ($count == 10){
			echo "<br>";
		}
			
}

?>

line 4 to 8 is pointless

I think i have it......

this gives 20 words per line

<?php
$str = "Hello world. It's a beautiful day. lots of words and stuff and things and all sorts of shite.  yada yada yada yada yada yada yada yada yada yada yada yada yada yada yada";
$str2 = (explode(" ",$str));
print count($str2);
echo "<br>";
$count = 0;
foreach ($str2 as $word){
	echo $word . " ";
	$count ++;
			if($count %20 == 0) {
    		echo "<br>";
		}
}

?>

I've solved it!!!!!

This script allows you to set a max number of characters and doesn't cut words in the middle!!!

<?php
/*
PHP 5 Required for function:
<http://uk3.php.net/str_split>
*/

$str = "Hello world. It's a beautiful day. lots of words and stuff and things and all sorts of shite.  yadaaa yaaada yada yaaada yada yada yada yada yada yada yada yada yada yada yada";
$str2 = (explode(" ",$str));
echo count($str2).' Words';
echo ' and '.strlen($str).' characters';

$limit = 100;  // Max of 100 characters per line
$split = str_split($str);

$x = 0;
$y = 0;
$t = 1;

while($x < count($split))
{
	if(!isset($line[$y]))
	{
		$line[$y] = "";
	}
	$line[$y] = $line[$y].$split[$x];
	
	if($x==$t*$limit-1)
	{
		if($split[$x] != " ")
		{
			/* Check if $x+1 is not a space... */
			if($split[$x+1] != " ")
			{
				$tmp = explode($line[$y], " ");
				$num = count($tmp);
				$num--;
				$tmp2 = "";
				
				$d   = 0;
				
				while($d <= $num)
				{
					$tmp2 = $tmp2.$tmp[$d];
					$d++;
				}
				
				$x = strlen($tmp2);
				$t = ($x+$limit)/$limit;
			}
		}
		$y++;
	}
	$x++;
}
print_r($line);

?>

If you don't have PHP 5, check out http://www.jonasjohn.de/snippets/php/split-by-length.htm

Kieran :)
YAY!

AH!

Didn't test it enough, that only works for line 1. Fixing the problem...

Well, I am going to leave it for tonight (Its late here).

Will work on it in the morning, this is very interesting ;)

Please post fix if found.

Kieran :)

OK, I know it's marked as solved now but I have found a function built into PHP that does this:

string wordwrap ( string source [, int width [, string break [, boolean cut]]])

Example:

<?php
    $text = "Word wrap will split this text up into smaller lines, which makes for easier reading and neater layout.";
    $text = wordwrap($text, 20, "\n");
    print $text;
?>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.