I recently how to display a text as columns with PHP.
Now, thanks to , which I have also , I have ported the same functionality to JavaScript.
No work from my side really other than using the corresponding PHP functions from PHP.JS and creating my own JavaScript split_pos function. I have also created a print_columns function that calls split_pos and renders the columns on the web page. Currently, as you can see, this function is pretty hard-coded and can only handle one and two columns. I will update this entry with a more abstract version if I get the time to do so.
You need to download and include the package from PHP.JS for this to work since several ported JavaScript functions from this library are being used.
function split_pos(text) { var mid; var cut; var part1; var pos1; var pos2; var pos3;
/* find middle space in text */ mid = strlen(text)/2 - 1; cut = strpos(text , ' ' , mid); part1= substr(text , 0 , cut + 1);
pos1 = strrpos(part1 , '<'); pos2 = strrpos(part1 , '>');
if ((pos1 < pos2) || (pos1 === false)) { return cut; /* no html tag around */ }
pos3 = strpos(text , '>' , cut1 + 1); if (pos3 !== false) { return pos3; /* end of middle html tag */ } else { return cut; /* unbalancing < > */ } }
function print_colums(text, column_spacing, column_width, columns) { var mid_pos = split_pos(text); var col_str = ""; col_str = "<table cellpadding='0' cellspacing='0'><tr valign='top'>"; if (columns == 1) { col_str = col_str + "<td class='textContent' style='width:" + column_width + "px'>" + text + "</td><td style='" + column_spacing + "px'></td><td class='textContent' style='width:" + column_width + "px'></td>"; } else if (columns == 2) { col_str = col_str + "<td class='textContent' style='width:" + column_width + "px'>" + text.substr(0,mid_pos) + "</td><td style='width:" + column_spacing + "px'></td><td class='textContent' style='width:" + column_width + "px'>" + text.substr(mid_pos + 1) + "</td>"; } col_str = col_str + "</tr></table>"; return col_str; } |