Skip to content

Instantly share code, notes, and snippets.

@harisrozak
Last active May 28, 2023 20:12
Show Gist options
  • Save harisrozak/3f0dbd104939e241f287 to your computer and use it in GitHub Desktop.
Save harisrozak/3f0dbd104939e241f287 to your computer and use it in GitHub Desktop.
PHP :: Simple PHP Excerpt
<?php
/**
* $pos = get cut position based on fixed position ($fixed),
* but without break last word
*/
$text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit.";
$excerpt = '';
$fixed = 25;
if(strlen($text) >= $fixed)
{
$pos = strpos($text, ' ', $fixed);
$pos = $pos ? $pos : strlen($text);
$excerpt = substr($text,0,$pos);
// delete last non alphanumeric character (save ">" if you want to save html tag)
$excerpt = preg_replace('/[`!@#$%^&*()_+=\-\[\]\';,.\/{}|":<>?~\\\\]$/', '', $excerpt);
}
$excerpt = $excerpt == '' ? $text : $excerpt;
echo fix_unclosed_html_tag($excerpt);
function fix_unclosed_html_tag($string) {
if(trim($string) == '') return '';
libxml_use_internal_errors(true);
$dom = new DOMDocument();
$dom->loadHTML(mb_convert_encoding($string, 'HTML-ENTITIES', 'UTF-8'));
return $dom->saveHTML($dom->getElementsByTagName('div')->item(0));
}
@xeoncross
Copy link

This doesn't work with unicode. Instead of removing non-ASCII/English letters/numbers just remove ending ,.'[]{}()!@#$%^&*()_+ etc...

@harisrozak
Copy link
Author

Hi Xeoncross, thanks for your help!, i have updated the code 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment