Last active
May 28, 2023 20:12
-
-
Save harisrozak/3f0dbd104939e241f287 to your computer and use it in GitHub Desktop.
PHP :: Simple PHP Excerpt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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)); | |
} |
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
This doesn't work with unicode. Instead of removing non-ASCII/English letters/numbers just remove ending
,.'[]{}()!@#$%^&*()_+
etc...