Location: PHPKode > scripts > Strings > strings/class.strings.php
<?PHP
/**
  * @author     Roberto Bertó darkelder (inside) users (dot) sourceforge (dot) net
  * @link       http://www.freshmeat.net/projects/phpclass-strings/
  * @copyright   {@link http://www.gnu.org/copyleft/lesser.html LGPL}
  *
  * ChangeLog:
  * 22/MAI/2002
  *     - changed php_quote() to php_remove()
  *     - added var $locale to strings class
  * 11/APR/2002
  *     - code cleaning
  *     - added locale and using PHP Strings functions when possible
  * 01/DEC/2000
  *     - creation
  *
  * TODO:
  *     - do not change the locale
  *     - remove not-transform-functions
  *     - add a function to check if a string have special chars
  *     - find a translation from portuguese "acentuação gráfica" to english
  *     - put preg expressions into a class variable
  *     - convert the documentation and the class comments to valid phpdocument
  *       (@link http://freshmeat.net/projects/phpdocu/)
  */

class strings
{
	// do not change because PT_BR is a contry that use special chars
	var $locale = "PT_BR";
	
	// set locale to $locale
	function strings()
	{
		setlocale (LC_CTYPE, $this->locale);
	}

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
	# PURPOSE: transform "ÉÁSSÍMMÊSMÒ" in "éássímmêsmò"
	# OLD FUNCTION, to learning how make a regular expression that convertets À to à, and so on:
		/*
		function lowercase($p)
		{
			return preg_replace("/([À-Ý]|[A-Z])/e","chr(ord('\\1')+32)", $p);
		}
		*/
	# EXAMPLE:
		/*
		$strings = new strings();
		print $strings->strtolower("ÉÁSSÍMMÊSMÒ");
		*/

	function strtolower($p) {
		return strtolower($p);
	}

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

	# PURPOSE: transform "éássímmêsmò" in "ÉÁSSÍMMÊSMÒ"
	# OLD FUNCTION, to learning how make a regular expression that convertets à to À, and so on:
		/*
		function uppercase($p)
		{
			return preg_replace("/([à-ý]|[a-z])/e","chr(ord('\\1')-32)", $p);
		}
		*/
	# EXAMPLE:
		/*
		$strings = new strings();
		print $strings->strtoupper("éássímmêsmò");
		*/

	function strtoupper($p)
	{
		return strtoupper($p);
	}

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

	# PURPOSE: transform "éassim" in "Éassim"
	# OLD FUNCTION, to learning how make a regular expression that convertets é to É, and so on:
		/*
		function ucfirst($p)
		{
			return preg_replace("/^([à-ý]|[a-z]){1,1}/e","chr(ord('\\1')-32)", $p);
		}
		*/
	# EXAMPLE:
		/*
		$strings = new strings();
		print $strings->ucfirst("éassim");
		*/

	function ucfirst($p) {
		return ucfirst($p);
	}

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

	# PURPOSE: transform "Éassim" in "éassim"
	# EXAMPLE:
		/*
		$strings = new strings();
		print $strings->lcfirst("Éassim");
		*/

	function lcfirst($p)
	{
		return preg_replace("/^([À-Ý]|[A-Z]){1,1}/e","chr(ord('\\1')+32)", $p);
	}

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

	# PURPOSE: transform "éssim in ónoris" in "Éssim In Ónoris"
	# OLD FUNCTION - to learn how create ucwords :-)
		/*
		function ucwords($p)
		{
			$c = explode(" ", $p);
			if ($c) {
				$d = array();
				while (list($a,$b) = each($c)) {
					array_push($d,preg_replace("/^([à-ý]|[a-z]){1,1}/e","chr(ord('\\1')-32)", $b));
				}
				return join(" ", $d);
			}
		}
		*/
	# EXAMPLE:
		/*
		$strings = new strings();
		print $strings->ucwords("éssim in ónoris");
		*/

	function ucwords($p)
	{
		return ucwords($p);
	}

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

	# PURPOSE: transform "ÉSSIM IN ÓNORIS" in "éSSIM iN óNORIS"
	# EXAMPLE:
		/*
		$strings = new strings();
		print $strings->lcwords("ÉSSIM IN ÓNORIS");
		*/

	function lcwords($p)
	{
		$c = explode(" ", $p);
		if ($c)
		{
			$d = array();
			while (list($a,$b) = each($c))
			{
				array_push($d,preg_replace("/^([À-Ý]|[A-Z]){1,1}/e","chr(ord('\\1')+32)", $b));
			}
			return join(" ", $d);
		}
	}

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

	# PURPOSE: transform "ÉSSIM IN ÓNORIS cáusè" in "ESSIM IN ONORIS cause"
	# EXAMPLE:
		/*
		$strings = new strings();
		print $strings->normal("ÉSSIM IN ÓNORIS cáusè");
		*/

	function normal($p)
	{
		$ts = array("/[À-Å]/","/Æ/","/Ç/","/[È-Ë]/","/[Ì-Ï]/","/Ð/","/Ñ/","/[Ò-ÖØ]/","/×/","/[Ù-Ü]/","/[Ý-ß]/","/[à-å]/","/æ/","/ç/","/[è-ë]/","/[ì-ï]/","/ð/","/ñ/","/[ò-öø]/","/÷/","/[ù-ü]/","/[ý-ÿ]/");
		$tn = array("A","AE","C","E","I","D","N","O","X","U","Y","a","ae","c","e","i","d","n","o","x","u","y");
		return preg_replace($ts,$tn, $p);
	}










             /* THE ABOVE FUNCTIONS ARE USEFULL BUT IS NOT MAINTED ANYMORE */











/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

	# PURPOSE: remove astring from another
	# EXAMPLE:
		/*
		$strings = new strings();
		print $strings->remove("We are the champions","We are");
		*/

	function remove($p,$c)
	{
		return str_replace($c,"",$p);
	}

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

	# PURPOSE: remove all \r or \n from the string
	# EXAMPLE:
		/*
		$strings = new strings();
		print "<PRE>" . $strings->chomp("We a\rre\n\rthe\r\ncha\nmpions\n\r");
		*/

	function chomp($p)
	{
		return preg_replace("/[\n\r]/","", $p);
	}

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

	# PURPOSE: remove php tags from a string
	# EXAMPLE:
		/*
		$strings = new strings();
		print $strings->php_remove("<script \nlanguage=\"php\">echo 'I'm hacking!';</script>");
		*/

	function php_remove($text,$a=1,$b=1,$c=0,$d=0)
	{
		if ($a) {
			$text = preg_replace("/\<\?(php)/i","<!?\\1",$text);
		}
		if ($b) {
			$text = preg_replace("/[^\!](\blanguage\b.*=.*php)/si","!\\1",$text);
		}
		if ($c) {
			$text = preg_replace("/\<\?/","<!?",$text);
		}
		if ($d) {
			$text = preg_replace("/\<\%/","<!%",$text);
		}
		return $text;
	}

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

	# PURPOSE: return the filename extension
	# EXAMPLE:
		/*
		$strings = new strings();
		print $strings->file_ext("sometext.doc");
		*/

	function file_ext($s)
	{
		if (preg_match("/([^\.]*$)/", $s,$p))
		{
			return $p[1];
		}
	}

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

	# PURPOSE: return the filename without extension
	# EXAMPLE:
		/*
		$strings = new strings();
		print $strings->file_noext("sometext.doc");
		*/

	function file_noext($s) {
		if (preg_match('/(.*)\..*$/',$s,$p))
		{
			return $p[1];
		}
	}


/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

	# PURPOSE: convert // into /, but preseve http:// and ftp://
	# EXAMPLE:
		/*
		$strings = new strings();
		print $strings->stripdouble("there was a lot of // and some http://foo.com");
		*/

	function stripdouble($s) {
		return preg_replace("/(htt|ft)p:\//","\\1p://",preg_replace('/\/{2,}/','/',$s));
	}


/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

	# PURPOSE: return lines number of a file
	# WARNING: I didnt rewrite this function! Take care with big files because it'll not work or will get too much server resources
	# EXAMPLE:
		/*
		$strings = new strings();
		print $strings->lines("c:/windows/win.ini");
		*/

	function lines($f) {
		return count(file($f));
	}

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

	# PURPOSE: turn / to \/
	# NOTE: there are PHP functions that do the same thing. But I'll leave this function here.
	# EXAMPLE:
		/*
		$strings = new strings();
		print $strings->bar_quote("/");
		*/

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

	function bar_quote($s) {
		return preg_replace("/\//","\/",$s);
	}
}

?>
Return current item: Strings