Revision: 4684
Updated Code
at January 15, 2008 11:25 by bioascii
Updated Code
/* Description: Function to limit the text with or without relating words and from the start or end of the string Usage: $string = txtlmt($string, "30", "...", "1"); Variables: $txt = string to delimited $txt_limiter = max amount of letters in the string that should be seen $txt_sep = the separator used to end the string (like string...) $tipo 1 - from start to end without relating words (when it reaches the $txt_limiter it breaks the string) 2 - from start to end relating words (when it reaches the $txt_limiter it finds the next space to break the string) 3 - from end to start without relating words (when it reaches the $txt_limiter it breaks the string) 4 - from edn to start relating words (when it reaches the $txt_limiter it finds the next space to break the string) */ function txtlmt ($txt,$txt_limiter,$txt_sep,$tipo=0) { if($tipo=="" OR $tipo==NULL OR $tipo==0) { $tipo = "2"; } if (strlen($txt) > $txt_limiter) { // From start to end if($tipo=="1") { // Do not related words $txt = substr(put_accents($txt), 0, $txt_limiter) . $txt_sep; } elseif($tipo=="2") { // Relate words $txt = substr(put_accents($txt), 0, $txt_limiter); $txt = substr($txt, 0, strrpos($txt, " ")) . $txt_sep; // From end to start } elseif($tipo=="3") { // Do not related words $txt = $txt_sep.substr(put_accents($txt), -$txt_limiter); } elseif($tipo=="4") { // Relate words $txt = substr(put_accents($txt), -$txt_limiter); $txt = $txt_sep.substr($txt, strpos($txt, " ")+1); } $txt = strip_accents($txt); return $txt; } else { return $txt; } } // ################################################################# // Function to convert strings to HTML characters function strip_accents ($string) { $string = ereg_replace("(á)","á",$string); $string = ereg_replace("(à )","à ",$string); $string = ereg_replace("(ã)","ã",$string); $string = ereg_replace("(ó)","ó",$string); $string = ereg_replace("(õ)","õ",$string); $string = ereg_replace("(é)","é",$string); $string = ereg_replace("(ú)","ú",$string); $string = ereg_replace("(Ã)","Ã",$string); $string = ereg_replace("(ç)","ç",$string); $string = ereg_replace("(Ã)","Ã",$string); $string = ereg_replace("(À)","À",$string); $string = ereg_replace("(Ã)","Ã",$string); $string = ereg_replace("(Ó)","Ó",$string); $string = ereg_replace("(Õ)","Õ",$string); $string = ereg_replace("(É)","É",$string); $string = ereg_replace("(Ú)","Ú",$string); $string = ereg_replace("(Ã)","Ã",$string); $string = ereg_replace("(Ç)","Ç",$string); $string = ereg_replace("(\")",""",$string); $string = ereg_replace("(<)","<",$string); $string = ereg_replace("(>)",">",$string); $string = ereg_replace("(`)","'",$string); $string = ereg_replace("(')","'",$string); $string = ereg_replace("º","º",$string); $string = ereg_replace("ª","ª",$string); return $string; } // ################################################################# // Function to convert strings back to HTML characters function put_accents ($string) { $string = ereg_replace("(á)","á",$string); $string = ereg_replace("(à )","à ",$string); $string = ereg_replace("(ã)","ã",$string); $string = ereg_replace("(ó)","ó",$string); $string = ereg_replace("(õ)","õ",$string); $string = ereg_replace("(é)","é",$string); $string = ereg_replace("(ú)","ú",$string); $string = ereg_replace("(Ã)","Ã",$string); $string = ereg_replace("(ç)","ç",$string); $string = ereg_replace("(Ã)","Ã",$string); $string = ereg_replace("(À)","À",$string); $string = ereg_replace("(Ã)","Ã",$string); $string = ereg_replace("(Ó)","Ó",$string); $string = ereg_replace("(Õ)","Õ",$string); $string = ereg_replace("(É)","É",$string); $string = ereg_replace("(Ú)","Ú",$string); $string = ereg_replace("(Ã)","Ã",$string); $string = ereg_replace("(Ç)","Ç",$string); $string = ereg_replace("(")","\"",$string); $string = ereg_replace("(<)","<",$string); $string = ereg_replace("(>)",">",$string); $string = ereg_replace("(')","'",$string); $string = ereg_replace("º","º",$string); $string = ereg_replace("ª","ª",$string); return $string; } // #################################################################
Revision: 4683
Initial Code
Initial URL
Initial Description
Initial Title
Initial Tags
Initial Language
at January 15, 2008 11:24 by bioascii
Initial Code
/* Description: Function to limit the text with or without relating words and from the start or end of the string Usage: $string = txtlmt($string, "30", "...", "1"); Variables: $txt = string to delimited $txt_limiter = max amount of letters in the string that should be seen $txt_sep = the separator used to end the string (like string...) $tipo 1 - from start to end without relating words (when it reaches the $txt_limiter it breaks the string) 2 - from start to end relating words (when it reaches the $txt_limiter it finds the next space to break the string) 3 - from end to start without relating words (when it reaches the $txt_limiter it breaks the string) 4 - from edn to start relating words (when it reaches the $txt_limiter it finds the next space to break the string) */ function txtlmt ($txt,$txt_limiter,$txt_sep,$tipo=0) { if($tipo=="" OR $tipo==NULL OR $tipo==0) { $tipo = "2"; } if (strlen($txt) > $txt_limiter) { // From start to end if($tipo=="1") { // Do not related words $txt = substr(put_accents($txt), 0, $txt_limiter) . $txt_sep; } elseif($tipo=="2") { // Relate words $txt = substr(put_accents($txt), 0, $txt_limiter); $txt = substr($txt, 0, strrpos($txt, " ")) . $txt_sep; // From end to start } elseif($tipo=="3") { // Do not related words $txt = $txt_sep.substr(put_accents($txt), -$txt_limiter); } elseif($tipo=="4") { // Relate words $txt = substr(put_accents($txt), -$txt_limiter); $txt = $txt_sep.substr($txt, strpos($txt, " ")+1); } $txt = strip_accents($txt); return $txt; } else { return $txt; } } // ################################################################# // Function to convert strings to HTML characters function strip_accents ($string) { $string = ereg_replace("(á)","á",$string); $string = ereg_replace("(à )","à",$string); $string = ereg_replace("(ã)","ã",$string); $string = ereg_replace("(ó)","ó",$string); $string = ereg_replace("(õ)","õ",$string); $string = ereg_replace("(é)","é",$string); $string = ereg_replace("(ú)","ú",$string); $string = ereg_replace("(Ã)","í",$string); $string = ereg_replace("(ç)","ç",$string); $string = ereg_replace("(Ã)","Á",$string); $string = ereg_replace("(À)","À",$string); $string = ereg_replace("(Ã)","Ã",$string); $string = ereg_replace("(Ó)","Ó",$string); $string = ereg_replace("(Õ)","Õ",$string); $string = ereg_replace("(É)","É",$string); $string = ereg_replace("(Ú)","Ú",$string); $string = ereg_replace("(Ã)","Í",$string); $string = ereg_replace("(Ç)","Ç",$string); $string = ereg_replace("(\")",""",$string); $string = ereg_replace("(<)","<",$string); $string = ereg_replace("(>)",">",$string); $string = ereg_replace("(`)","'",$string); $string = ereg_replace("(')","'",$string); $string = ereg_replace("º","º",$string); $string = ereg_replace("ª","ª",$string); return $string; } // ################################################################# // Function to convert strings back to HTML characters function put_accents ($string) { $string = ereg_replace("(á)","á",$string); $string = ereg_replace("(à)","à ",$string); $string = ereg_replace("(ã)","ã",$string); $string = ereg_replace("(ó)","ó",$string); $string = ereg_replace("(õ)","õ",$string); $string = ereg_replace("(é)","é",$string); $string = ereg_replace("(ú)","ú",$string); $string = ereg_replace("(í)","Ã",$string); $string = ereg_replace("(ç)","ç",$string); $string = ereg_replace("(Á)","Ã",$string); $string = ereg_replace("(À)","À",$string); $string = ereg_replace("(Ã)","Ã",$string); $string = ereg_replace("(Ó)","Ó",$string); $string = ereg_replace("(Õ)","Õ",$string); $string = ereg_replace("(É)","É",$string); $string = ereg_replace("(Ú)","Ú",$string); $string = ereg_replace("(Í)","Ã",$string); $string = ereg_replace("(Ç)","Ç",$string); $string = ereg_replace("(")","\"",$string); $string = ereg_replace("(<)","<",$string); $string = ereg_replace("(>)",">",$string); $string = ereg_replace("(')","'",$string); $string = ereg_replace("º","º",$string); $string = ereg_replace("ª","ª",$string); return $string; } // #################################################################
Initial URL
Initial Description
This function allows you to limit the amount of a string, like a sample from a post. Create a file like function_lmt_txt.php and include it to use it. Any comments to improve this are welcomed.
Initial Title
Limit string with/without relating words and from start/end
Initial Tags
php
Initial Language
PHP