I need to split a big chunk of text into paragraphs of the same size, with the exact same number of string lines to keep them uniforms, i’m currently doing it with php with the following code.
function arrangeText(){
if (have_posts()) : while (have_posts()) : the_post();
$content = strtolower(get_the_content());
$content = trim(strip_tags($content));
$post_len = strlen($content);
$segmentCont = 1;
$auxOffset = 0;
$auxOffset2 = 0;
$limit = 896;
$limitTotal = 0;
while($limitTotal<$post_len){
if($segmentCont==2) {
$limit = 943;
}
$segmentCont++;
$limitTotal = $limitTotal + $limit;
if($limitTotal<$post_len)
{ $auxOffset = strpos($content," ",$limitTotal);}
else
{ $auxOffset = $post_len;}
$post_segment = trim(substr($content,$auxOffset2,$auxOffset-$auxOffset2));
$auxOffset2 = $auxOffset;
echo_segment($post_segment);
}
endwhile;
endif;
}
This actually works but even though im counting each character, some characters may take up to three character spaces, so my paragraphs get different number of lines, or the last line is sliced in half, if i use a monotypefont everything goes smoothly, but my client wants a specific font, and i’ve been trying all sort of things with no luck, so i come to you the big guys for help. The only thing i can imagine to solve this is a very complex script to get the width of each character im echoing and add that value to a counting variable each time, and do a conditional (inside a loop i guess) to check if the total width of the characters exceeds the total width of my paragraph if so slice it before the last ” “(white space) it founds(preventing the code to cut words in half), but i have no idea if this is possible with php, js, jquery or anything that can help me out on a website, im sure there is an easy way to do what i’m trying here but maybe im just not smart enough, so pls help me out =).
Edit 1: My bad, the mono-spaced font won’t do the trick either, they were getting cut and overflowing below my p tags ( overflow:hidden ) and i though they were working until i read the text, so i guess that even that they are mono-spaced, their character sizes are different too.
You can use this code to measure widths of characters and create a “dictionary”.
You can tweak it to return relative values (lets say
w = 1,i = 0.5so it will be more flexible).The fiddle is here: http://jsfiddle.net/Gw4AV/