Requested by Commenter: dawoodzai
Original Post: Limit Characters in a PHP String
So I just got a comment from dawoodzai asking if I could turn the Limit Characters in a PHP String article into a php function, well it was more like “I want to convert this to a function, … Thanks”, anyway… I made it into a function and added 2 new features, Preserve Tags and Auto Close Open Tags. Preserve Tags is my function/logic but I have to give credit to Jamie Wilkinson and the Code Snippet from CodeSnippets.Joyent.com for his auto detect and close tags function [ JamieW's Code Snippets Profile ].
Other resources
- Limit the middle characters of a string – Useful for preview of unimportant text (reminds me of firebug)
- Limit length of a text by not cutting full words – It’s my function on steroids and it doesn’t cut up the last word. However the number of characters will very from string-to-string
<?php
$sample_text = '<strong>Lorem ipsum</strong> dolor sit amet, consectetur adipiscing elit.
Morbi sapien sem, elementum id viverra ac, posuere ac magna. Fusce ut nisl nisl. Nullam non tellus
imperdiet dolor pulvinar pretium id id orci. Duis egestas laoreet enim vel malesuada. Pellentesque
habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Donec ut justo
nibh venenatis consequat non a urna. Vivamus quis mi leo. <em>Nullam velit mi, posuere id
mollis i, luctus pretium felis. Iun hac habitasse platea dictumst</em>. Quisque suscipit suscipit
mi et lobortis. Integer a sem sed felis interdum ultrices vitae at velit. Donec purus metus, luctus eu
dapibus ihn, bibendum eu mauris. Aliquam scelerisque sem pin dolor egestas at pellentesque est
rhoncus. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean vulputate <a href="#">ipsum</a>
ac massa vulputate consequat. Fusce quis consequat nunc. Class aptent taciti sociosqu ad litora
torquent per conubia nostra, per inceptos himenaeos. Phasellus iaculis justo eget mi cursus porta.';
echo strlimit($sample_text, 400, 0);
//$strlimit(String to edit, Limit of Charactars, Preserve Tags [1 = true, 0 = false])
function strlimit($string, $limit, $tags = false) {
$pre_content = strip_tags( $string );
$content_length = strlen($pre_content);
$content = '';
if($tags) {
$tag_length = strlen($string);
$limit = $limit + ($tag_length - $content_length);
unset($pre_content);
$pre_content = '';
$content = $string;
}
$content .= $pre_content;
if($content_length <= $limit) {
$thisexcerpt = '<div class="excerpt">' . $tcontent . '</div>';
}else{
$new_length = $content_length - $limit;
$thisexcerpt = '<div class="excerpt">' . close_dangling_tags(substr($content, 0, -$new_length));
$thisexcerpt .= '...</div>';
};
return $thisexcerpt;
}
function close_dangling_tags($html){
#put all opened tags into an array
preg_match_all("#<([a-z]+)( .*)?(?!/)>#iU",$html,$result);
$openedtags=$result[1];
#put all closed tags into an array
preg_match_all("#</([a-z]+)>#iU",$html,$result);
$closedtags=$result[1];
$len_opened = count($openedtags);
# all tags are closed
if(count($closedtags) == $len_opened){
return $html;
}
$openedtags = array_reverse($openedtags);
# close tags
for($i=0;$i < $len_opened;$i++) {
if (!in_array($openedtags[$i],$closedtags)){
$html .= '</'.$openedtags[$i].'>';
} else {
unset($closedtags[array_search($openedtags[$i],$closedtags)]);
}
}
return $html;
}
?>