This snippet allows you to manipulate the amount of characters that are displayed on screen. You can search for any character or set of characters: eg HTML tags.
<?php
/*
Modified version of snippets found at: http://forums.devshed.com/showthread.php?t=76514
*/
// get an abbreviated version of some text
function limitedDesc($haystack, $needle, $len)
{
$output = "";
$charlen = strlen($needle);
$strlen = strlen($haystack);
/* NEW 2008/01/10 */
$clean_strlen = strlen(strip_tags($haystack));
$len = ($strlen - $clean_strlen) + $len;
for($i = 0; $i < $strlen; $i++):
$char_num = $len + $i;
$new_str = substr($haystack, 0, $char_num);
$last_char = substr($new_str, -$charlen);
if($last_char == $needle):
$output = $new_str;
break;
endif;
endfor;
if($output == ""):
$output = $haystack;
endif;
return $output;
}
// what you are trying to abbreviate
$haystack = '<a href="yadaydayadaydayadaydayadayda">Lorem ipsum dolor</a> sit amet,
consectetuer adipiscing elit, sed <a href="verylongurlsareincludedinthistextnow">diam</a>
nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat.
Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut
aliquip ex ea commodo consequat. Duis autem vel eum iriure dolor
in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat
nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit
praesent <a href="morelinksbutirrelevantnow">luptatum zzril delenit a</a>';
// what you want the last character to be
$needle = ' ';
// how many characters do you want
$len = 50;
Comments
cool but ..
Thanks that is a great and easy script.
but what if some of your text includes a long url path. is it possible to not count that in the limit ?
thanks
Very simple way to do this
You can just make the $len longer automatically by comparing the text lengths with and without html tags....
I have edited the code above to incorporate this. Hope it helps.
Br
Jamie
Unraveling the mysteries of the web
http://www.skiffie.com