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

Imaginx
Imaginx used Ask the Experts™
on
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
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Greg AlexanderLead Developer

Commented:
You can explode on something like commas

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

Author

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

Author

Commented:
I see what you're saying ... I supposed I could do something like that ...
Learn Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

Greg AlexanderLead Developer

Commented:
I am writting something more friendly ... one sec
Top Expert 2011
Commented:
<?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.

Author

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 ?
Greg AlexanderLead Developer

Commented:
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

Greg AlexanderLead Developer

Commented:
With the above, you could split on the number of words you set in the function

Author

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?
Greg AlexanderLead Developer

Commented:
Nope... his is cleaner!

Author

Commented:
Thanks galexander07 !!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial