More String Functions in PHP
As you know by now, string functions are very useful in manipulating a string. The list of predefined string functions is huge, so we will discuss only mostly commonly used string functions.
Strlen: This string function will return length of a string.
Ucfirst: This string function will change case of first letter of the string to upper.
Ucwords: This string function will change case of the first letter of every word in the string to upper.
Trim: This string function will remove whitespaces or any predefined characters from both ends of a string.
Strstr: This string function will search for an input string in a given string and if input string is found, it will return rest of the string followed by input string.
Str_repeat: This string function will repeat a given string. It will take a string and integer value as parameter. Integer value represents how many times you want to repeat that string.
Substr: This string function will return a sub string in a given string. It will take as parameters a string, integer value as start of index, and another integer value as number of characters to return.
Strpos: This string function will return index position of input string in a given string.
Strchr: This string function is pretty much similar to strstr. This will return an entire string followed by a specific character.
Str_replace: This string function will replace an input string in a given string.
<?php $str = "my PHP tutorials for beginners "; //strlen example echo strlen($str)."<br />"; //ucfirst example echo ucfirst($str)."<br />"; //ucwords example echo ucwords($str)."<br />"; //trim example echo trim($str)."<br />"; //strstr example echo strstr($str,"for")."<br />"; //str_repeat example echo str_repeat($str, 3)."<br />"; //substr example echo substr($str, 3, 10)."<br />"; //strpos example echo strpos($str,"PHP")."<br />"; //strchr example echo strchr($str,"o")."<br />"; //str_reolace example echo str_replace("PHP", "FWAIT", $str)."<br />"; ?>