Breaking a block of text in half.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Breaking a block of text in half.

Post 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?
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

You'll have to parse the text and disclude the HTML.
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post 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
Post Reply