views:

778

answers:

2

I want to take the content of a website article and create two or more columns of text. The difficult part is that i must keep the html tags and also close them if the cutting is done inside a <p></p> for example.

Example:

<p><span>One two three <strong>four</strong></span> five six</p>

Result:

<p><span>One two three<span><p>
<p><span><strong>four</strong> five six</p>

So the user will se

One two three 
four five six
+2  A: 

Hi,

You have to track the opened tags in a stack data structure. Then when you reach your split point, you have to close every tag that is on the stack in that exact order. The same for openening tags in the next string (in reverse order).

Martin Lazar
A: 

If you're going to use javascript, I would suggest http://welcome.totheinter.net/columnizer-jquery-plugin/

MartinodF
i am more interested in a php algorithm. 10x
Daniel
You should start with preg_split("/<.*?\/?>", $text, PREG_SPLIT_DELIM_CAPTURE); so that you have an array with text and html tags. Then parse the entire array, storing opened tags and closed ones (mind self closing tags <br />). If $text is user input then you should watch out for things like <a><b>foo</a></b>, or <a><b>foo</a> and so on. Not so simple, but I did something similar writing a phpBB parser :)
MartinodF
thanks, i was just shearcing for something like that :)
Daniel