note 36238 deleted from function.substr by didou
| From: | didou@php.net | Date: | Thu, 02 Oct 2003 09:01:15 +0000 |
| Subject: | note 36238 deleted from function.substr by didou | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-57592@lists.php.net to get a copy of this message | ||
Note Submitter: SHI (shi@lineargSPAMISEVIL.com)
----
I was surprised to find that there was not much help around when it comes to trimming HTML formatted
text using substr().
My problem was that I didn't want my Text trimmed in the middle of a tag, or before a tag was
closed (hence changing the content following).
e.g. Trimming "Hi there <B>Billy</B>." anywhere after the 8th character would
create serious problems.
Anyhoo, I've created a simple function which trims a String to your desired length, preserves
HTML tags and formatting, cleans up redundant tags & doesn't crop the last word in the
String.
########################################################
# TRIM HTML TEXT #
########################################################
$text = The Text String to be Trimmed
$maxTextLenght = The Length of the Desired Output String
$end_string = A String which is appended to end of $text only if $text < $maxTextLenght
(Optional)
########################################################
function trimHtml($text,$maxTextLenght,$end_string="...") {
//TRIM HTML FORMATTED TEXT WITHOUT BREAKING TAGS
$trimmed_text=""; //THE NEW TRIMMED STRING
$tag=0; //INSIDE OR OUTSIDE TAG IDENTIFIER (0=Outside 1=Inside)
if(strlen($text)>$maxTextLenght) { //IF $text IS LONGER THAN $maxTextLenght
for($i = 0; $i < strlen($text); $i++) {
if($text{$i}=="<") { $tag=1; } //START OF TAG
//ADD CHARACTER TO $trimmed_text IF WITHIN $maxTextLenght OR INSIDE TAG
if((($i<=$maxTextLenght) OR $tag==1) OR !isset($trim_position)) {
$trimmed_text.=$text{$i};
}
if($text{$i}==">") { $tag=0; } //END OF TAG
//GET POSITION OF LAST UN-TRIMMED CHARACTED
if(!isset($trim_position) AND $i>$maxTextLenght AND $tag==0 AND $text{$i}==" ") {
$trim_position=$i;
}
}
//SPLIT $trimmed_text AT TRIM POSITION AND CLEEN UP REDUNDANT CODE (REMOVING
'<BR>') FROM 2ND PORTION
$portion1=trim(substr($trimmed_text,0,$trim_position));
$portion2=trim(substr($trimmed_text,$trim_position));
$portion1=strip_tags($portion1,
'<A><B><I><U><S><FONT><IMG><BR><P>');
//INCLUDE SUPPORTED TAGS HERE
$portion2=strip_tags($portion2,
'<A><B><I><U><S><FONT><P>');
$portion2=str_replace("> <","><",$portion2); //REMOVE EMPTY SPACES
BETWEEN TAGS
$portion2=str_replace("<B></B>","",$portion2); //REMOVE REDUNDANT
BOLD TAGS
$portion2=str_replace("<U></U>","",$portion2); //REMOVE REDUNDANT
UNDERLINE TAGS
$portion2=str_replace("<I></I>","",$portion2); //REMOVE REDUNDANT
ITALIC TAGS
$portion2=str_replace("<S></S>","",$portion2); //REMOVE REDUNDANT
STRIKEOUT TAGS
$portion2=str_replace("<P></P>","",$portion2); //REMOVE REDUNDANT
PARRAGRAPH TAGS
$text=$portion1.$portion2.$end_string;
}
return $text;
}
I use this function all the time, so I hope it helps a few of you.