Link to home
Start Free TrialLog in
Avatar of Imaginx
Imaginx

asked on

Split a large piece of text into an array by rows.

What function can I use to turn a string like this:
$bodyText="Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words,"

into something like this:
$bodyArray=([0]="Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a",[1]="piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a",[2]="Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin",[3]="Latin words,");

This will make life WAY easier with TCPDF ... TIA Experts
Avatar of Greg Alexander
Greg Alexander
Flag of United States of America image

You can explode on something like commas

$array = explode(",",$bodyText);

Avatar of Imaginx
Imaginx

ASKER

explode doesn't achieve the desired results. it puts each word into it's own array place or value.
Avatar of Imaginx

ASKER

I see what you're saying ... I supposed I could do something like that ...
I am writting something more friendly ... one sec
ASKER CERTIFIED SOLUTION
Avatar of Rik-Legger
Rik-Legger
Flag of undefined image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Imaginx

ASKER

I just tried it out with a slight more abstract character so there no chance of moving to the next element over a mis-interpreted common character ...

 
$bodyColY+=14;
$bodyArray=explode('::',$bodyText);
foreach($bodyArray as $line){
	$pdf->CreateTextBox($line, 25, $bodyColY+=4, 15, 10, 10, 'N', 'L');
}

Open in new window


Seems to work alright .. Never thought creative enough to use explode like that ...

What else did you have in mind ?
Consider this
<?
$bodyText="Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words,";

echo "<pre>";
print_r(breakup($bodyText,4));
echo "</pre>";

function breakup($text,$break_after){
	$words = explode(" ",$text);
	$new_string = '';
	$i = 0;
	foreach($words as $k => $v){
		$new_string .= $v." ";
		if($i % $break_after == 0 && $i != 0){
			$new_string .= "|";
		}
	$i++;
	}
	
	return explode("|",$new_string);
}


?>

Open in new window

With the above, you could split on the number of words you set in the function
Avatar of Imaginx

ASKER

galexander07, is there an advantage to the function you wrote over Rik's method ?

After correcting the $wrapped/$newtext variable in Rik's it worked - is there a value to the function?
Nope... his is cleaner!
Avatar of Imaginx

ASKER

Thanks galexander07 !!