/ Published in: PHP
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.
Create a file like function_lmt_txt.php and include it to use it.
Any comments to improve this are welcomed.
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
/* 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"; } // From start to end if($tipo=="1") { // Do not related words } elseif($tipo=="2") { // Relate words // From end to start } elseif($tipo=="3") { // Do not related words } elseif($tipo=="4") { // Relate words } $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); 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; } // #################################################################