Return to Snippet

Revision: 4684
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
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("(á)","&aacute;",$string);
	$string = ereg_replace("(à)","&agrave;",$string);
	$string = ereg_replace("(ã)","&atilde;",$string);
	$string = ereg_replace("(ó)","&oacute;",$string);
	$string = ereg_replace("(õ)","&otilde;",$string);
	$string = ereg_replace("(é)","&eacute;",$string);
	$string = ereg_replace("(ú)","&uacute;",$string);
	$string = ereg_replace("(í)","&iacute;",$string);
	$string = ereg_replace("(ç)","&ccedil;",$string);
	$string = ereg_replace("(Á)","&Aacute;",$string);
	$string = ereg_replace("(À)","&Agrave;",$string);
	$string = ereg_replace("(Ã)","&Atilde;",$string);
	$string = ereg_replace("(Ó)","&Oacute;",$string);
	$string = ereg_replace("(Õ)","&Otilde;",$string);
	$string = ereg_replace("(É)","&Eacute;",$string);
	$string = ereg_replace("(Ú)","&Uacute;",$string);
	$string = ereg_replace("(Í)","&Iacute;",$string);
	$string = ereg_replace("(Ç)","&Ccedil;",$string);
	$string = ereg_replace("(\")","&quot;",$string);
	$string = ereg_replace("(<)","&lt;",$string);
	$string = ereg_replace("(>)","&gt;",$string);
	$string = ereg_replace("(`)","'",$string);
	$string = ereg_replace("(')","&#039;",$string);
	$string = ereg_replace("º","&ordm;",$string);
	$string = ereg_replace("ª","&ordf;",$string);
	return $string;
}

// #################################################################



// Function to convert strings back to HTML characters
function put_accents ($string) {
	$string = ereg_replace("(&aacute;)","á",$string); 
	$string = ereg_replace("(&agrave;)","à",$string); 
	$string = ereg_replace("(&atilde;)","ã",$string); 
	$string = ereg_replace("(&oacute;)","ó",$string); 
	$string = ereg_replace("(&otilde;)","õ",$string); 
	$string = ereg_replace("(&eacute;)","é",$string); 
	$string = ereg_replace("(&uacute;)","ú",$string); 
	$string = ereg_replace("(&iacute;)","í",$string); 
	$string = ereg_replace("(&ccedil;)","ç",$string); 
	$string = ereg_replace("(&Aacute;)","Á",$string); 
	$string = ereg_replace("(&Agrave;)","À",$string); 
	$string = ereg_replace("(&Atilde;)","Ã",$string); 
	$string = ereg_replace("(&Oacute;)","Ó",$string); 
	$string = ereg_replace("(&Otilde;)","Õ",$string); 
	$string = ereg_replace("(&Eacute;)","É",$string); 
	$string = ereg_replace("(&Uacute;)","Ú",$string); 
	$string = ereg_replace("(&Iacute;)","Í",$string); 
	$string = ereg_replace("(&Ccedil;)","Ç",$string); 
	$string = ereg_replace("(&quot;)","\"",$string);
	$string = ereg_replace("(&lt;)","<",$string);
	$string = ereg_replace("(&gt;)",">",$string);
	$string = ereg_replace("(&#039;)","'",$string);
	$string = ereg_replace("&ordm;","º",$string);
	$string = ereg_replace("&ordf;","ª",$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