Automatically create row summary

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.

+4
source share
2 answers

You can use wordwrap and then count how many lines there are in the resulting string. If more than one, your text was longer than necessary, so you add a separator to the end of the first line and discard other lines. If there is only one line, your text was shorter, so there was no trimming.

It seems that wordwrap does not know utf8, but there is a comment that shows the utf8_wordwrap working function.

+2
source

You can also build a text summation algorithm written on a paper generalization based on extraction using the shortest path algorithm . This approach is not very difficult to implement.

Good luck

+2
source

Source: https://habr.com/ru/post/1342289/


All Articles