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
$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
ASKER
explode doesn't achieve the desired results. it puts each word into it's own array place or value.
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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 ...
Seems to work alright .. Never thought creative enough to use explode like that ...
What else did you have in mind ?
$bodyColY+=14;
$bodyArray=explode('::',$bodyText);
foreach($bodyArray as $line){
$pdf->CreateTextBox($line, 25, $bodyColY+=4, 15, 10, 10, 'N', 'L');
}
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);
}
?>
With the above, you could split on the number of words you set in the function
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?
After correcting the $wrapped/$newtext variable in Rik's it worked - is there a value to the function?
Nope... his is cleaner!
ASKER
Thanks galexander07 !!
$array = explode(",",$bodyText);