Page 1 of 1

Breaking a block of text in half.

Posted: Sat Nov 12, 2005 2:38 pm
by onion2k
I need to split a block of text in to two halfs. I can do this pretty easily using:

Code: Select all

$midpoint = (strlen($text)/2);
	$breakpoint = strpos($text, ". ", $midpoint);
	$firsthalf = substr($text, 0, $breakpoint+1);
	$secondhalf = substr($text, $breakpoint+1);
However, this only works for plain text. If there's HTML in there it all goes horribly wrong. Anyone got any ideas how to split text into two roughly equal halves and still maintain any HTML in it?

Posted: Sat Nov 12, 2005 2:42 pm
by Jenk
You'll have to parse the text and disclude the HTML.

Posted: Sat Nov 12, 2005 4:03 pm
by shiznatix
here is what i would do

split the text into a array by word but keep the word that has html next to it as 1 word ie

<b>text --- would stay as a word

then split it like that. you might run into troubles with links or images or what have you. come up with a forumula for your situation but that is how i would go about it