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
LVL 1
ImaginxAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Greg AlexanderLead DeveloperCommented:
You can explode on something like commas

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

0
ImaginxAuthor Commented:
explode doesn't achieve the desired results. it puts each word into it's own array place or value.
0
ImaginxAuthor Commented:
I see what you're saying ... I supposed I could do something like that ...
0
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

Greg AlexanderLead DeveloperCommented:
I am writting something more friendly ... one sec
0
Rik-LeggerCommented:
<?php

$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,";

$wrapped = wordwrap($bodyText, 30, "\n", true);

$array = explode("\n", $newtext);

print_r($array);

?>

Open in new window


I think you would be able to work with this.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ImaginxAuthor Commented:
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 ?
0
Greg AlexanderLead DeveloperCommented:
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

0
Greg AlexanderLead DeveloperCommented:
With the above, you could split on the number of words you set in the function
0
ImaginxAuthor Commented:
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?
0
Greg AlexanderLead DeveloperCommented:
Nope... his is cleaner!
0
ImaginxAuthor Commented:
Thanks galexander07 !!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.