To enter lines, we need to create a very simple summary form, trimming the end of the lines to a given length.
Here is the first version feature:
// Take an array of strings and generate a summary within a given length function stringSummaryFromMetadata($inArray,$len=80,$sep='Β§'){ // Filter out 'false' values $inputs=array_filter($inArray); // First try just imploding array $res=implode($sep,$inputs); // Check for length if(mb_strlen($res, 'utf8')>$len){ // Calculate 'z' the fixed width constant $x=count($inputs); $z=round(($len-$x)/$x); // Snip all strings to 'z' $t1=array(); foreach($inputs as $i) $t1[]=mb_substr($i,0,$z); // Final answer $res=implode($sep,$t1); } return $res; }
Test:
$test=array( 'Ligula diam risus tempus lorem sit', 'Cursus metus commodo enim odio orci', 'Metus sapien porta sapien fusce sodales', 'king queen' ); $out=stringSummaryFromMetadata($test); print $out;
What gives:
Ligula diam risus tΒ§Cursus metus Β§ commod β β β β β β β>
Good, but it can be much more optimal. I am sure about that. For example, the test result is less than 80 letters, spaces at the end of the line after trimming, words are interrupted, etc.
Before I leave for the tangent and jump in my own, I would like to ask the community if this has been set before and / or if an algorithm already exists for this.
source share