How to truncate a string in PHP to a sentence closest to a certain number of characters?

I want to truncate / shorten my string to a sentence closest to the number of ceratain characters.

I have a working function, but my function truncates the word closest to the number of certaion characters instead.

function shortenString($string, $your_desired_width) { $parts = preg_split('/([\s\n\r]+)/', $string, null, PREG_SPLIT_DELIM_CAPTURE); $parts_count = count($parts); $length = 0; $last_part = 0; for (; $last_part < $parts_count; ++$last_part) { $length += strlen($parts[$last_part]); if ($length > $your_desired_width) { break; } } return implode(array_slice($parts, 0, $last_part)); } 

For instance:

Lorem ipsum dolor sit amet, consectetur adipiscing elit. integer malesuada eleifend orci, eget dignissim ligula porttitor cursus. Devotee in pale enim. Curriculum Vitae natoque penatibus et magnis dis parturient montes, nascetur ridiculus shells Maecenas pulvinar gravida tempor.

Shortens to:

Lorem ipsum dolor sit amet, consectetur adipiscing elit. integer malesuada eleifend orci, eget dignissim ligula porttitor cursus.

Instead of breaking the sentence as follows:

Lorem ipsum dolor sit amet, consectetur adipiscing elit. integer malesuada eleifend orci, eget dignissim ligula porttitor cursus. Tradition in

Help is appreciated.

+6
source share
3 answers

This is what I came up with ... you have to check if the offer is longer than what you are looking for .. among other things, like what g13n said. Perhaps it would be better if the sentence is too short / long to chop it off and put "...". In addition, you will have to check / convert spaces, since strrpos will only look for what is given.

 $maxlen = 150; $file = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer malesuada eleifend orci, eget dignissim ligula porttitor cursus. Praesent in blandit enim. Maecenas vitae eleifend est. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Maecenas pulvinar gravida tempor."; if ( strlen($file) > $maxlen ){ $file = substr($file,0,strrpos($file,". ",$maxlen-strlen($file))+1); } 

if you want to use the same function, you can try the following:

 function shortenString($string, $your_desired_width) { $parts = preg_split('/([\s\n\r]+)/', $string, null, PREG_SPLIT_DELIM_CAPTURE); $parts_count = count($parts); $length = 0; $last_part = 0; $last_taken = 0; foreach($parts as $part){ $length += strlen($part); if ( $length > $your_desired_width ){ break; } ++$last_part; if ( $part[strlen($part)-1] == '.' ){ $last_taken = $last_part; } } return implode(array_slice($parts, 0, $last_taken)); } 
+4
source

You can simply use a simple regular expression, for example /^([^.]*?).*/ , and replace it with "$ 1". How:

 $output = preg_replace('/^([^.]+).*/', '$1.', $input); 

However, you should be aware that not all languages ​​have a period (.) As a sentence separator.

NTN.

+2
source

I tried several functions and regular expressions, but none of them work the way I would like, so I create this file:

 function sentenceTrim($string, $maxLength = 300) { $string = preg_replace('/\s+/', ' ', trim($string)); // Replace new lines (optional) if (mb_strlen($string) >= $maxLength) { $string = mb_substr($string, 0, $maxLength); $puncs = array('. ', '! ', '? '); // Possible endings of sentence $maxPos = 0; foreach ($puncs as $punc) { $pos = mb_strrpos($string, $punc); if ($pos && $pos > $maxPos) { $maxPos = $pos; } } if ($maxPos) { return mb_substr($string, 0, $maxPos + 1); } return rtrim($string) . '&hellip;'; } else { return $string; } } 

It trims the string to the specified maximum length, finds the last occurrence of the end (. Or! Or?) Of the last sentence from that string, and truncates this event again. It returns one or more complete sentences as close as possible to the specified number of characters.

Please correct my English.

+2
source

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


All Articles