Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Split a string into two columns

Status
Not open for further replies.

Quasibobo

Programmer
Oct 11, 2001
168
NL
Hi there,

My question isn't that difficult i guess, the answer however remains to be seen.... On a website I'm building I use a rather large body text. In the new lay-out of the site there are two columns in which the text can be display.

I would like to be able to split the bodytext string into two variables. Of course it would be the most beautifull solutions if the split occurs at the end of a sentence.

Please help!

P.S. I got on the way by counting the caracters of the string and do someting with substr() e.g. but that gave me the problem of breaking into the middle of a sentence of word....

Don't eat yellow snow!
 
to do this, I've used your question as text .

Code:
<?php
$string=addslashes("My question isn't that difficult i guess, the answer however remains to be seen.... On a website I'm building I use a rather large body text. In the new lay-out of the site there are two columns in which the text can be display.

I would like to be able to split the bodytext string into two variables. Of course it would be the most beautifull solutions if the split occurs at the end of a sentence.

Please help!

P.S. I got on the way by counting the caracters of the string and do someting with substr() e.g. but that gave me the problem of breaking into the middle of a sentence of word.");

$new=explode('. ',$string);

$count=count($new);

echo "part 1 ---------------------------------------<br>";
$i=0;
while($i< ($count / 2)){

	echo "[$i] $new[$i]<br>";

	$i++;
}

echo "<hr>";

$i=($count / 2);

echo "part 2 ---------------------------------------<br>";

while($i < $count){

	echo "[$i] $new[$i]<br>";

	$i++;

}

?>

______________________________________________________________________
There's no present like the time, they say. - Henry's Cat.
 
nuts, also need to remove those unsightly slashes again .. change the echo line in the while() loops to :
Code:
	echo "[$i] ". stripslashes($new[$i]) ."<br>";

______________________________________________________________________
There's no present like the time, they say. - Henry's Cat.
 
Thx,

That's a nice example, but is doesn't exactly do the trick. That's due to me not mentioning the columns aren't equal in size. In the right column there will always be a visual(image) displayed.

So the left column can contain about 1000 characters or 150/180 words more than the right column.

Now what?

Don't eat yellow snow!
 

divide the fist by 3 instead of 2 :p play with the code, use the force...

______________________________________________________________________
There's no present like the time, they say. - Henry's Cat.
 
Tnx,

I also tried something myself. I created a function which splits the text.

<?php
function columns($text, $part)
{
// Define where about you want to split the text.
$split_word = 400;
$amount_words = strlen($text);
// Define first part
$part_1 = substr($text, 0 , $split_word);
// Retrieve the last "." And rest of the sentence...
$left_over = strrchr($part_1,".");
// Lenght of left_over
$length_lover = strlen($left_over);
// Delete the left_over string out of the first part
$part_1 = str_replace($left_over, "", $part_1);
// Create part two by pasting left_over and the rest of the text
$part_2 = str_replace(". ", "", $left_over);
//$part_2 = str_replace(". ", "", strrchr($part_1,"."));
$part_2 .= substr($text, $split_word, $amount_words);

if ($part == 1)
{
return $part_1.".";
}
else if ($part == 2)
{
return $part_2;
}
}

?>

To call the function simply use this:
<?
echo columns($voorbeeld, "1");
?>

Nevertheless thnx for the direction in which to look at.


Don't eat yellow snow!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top