Home > Tutorials > PHP > String Functions
String Functions
Strings work different in every programming language. Here's a list of commonly used built-in string functions you may require:
| Function | Usage | Example |
|---|---|---|
| explode(seperator,string,limit) | Breaks up a string into an array, separated by the seperator (duh). limit, an optional parameter, will limit the maximum number of array elements. | explode(" ","The quick brown fox jumped over the crazy dog.");=array("The","quick","brown","fox","jumped","over","the","lazy","dog.") |
| implode(separator,array) | The opposite of explode, will form a string based on an array and the separator you pass. | $arr=array("Hello","World!");="Hello World!"; |
| ltrim(str) | Removes leading whitespace from a string | ltrim(" PHP");="PHP" |
| rtrim(str) | Removes trailing whitespace from a string | rtrim("PHP ");="PHP" |
| trim(str) | Removes leading and trailing whitespace from a string | trim(" PHP ");="PHP" |
| nl2br(str) | Converts line breaks to HTML <br/> tags. | nl2br("Hi.\nBye.");=Hi.Bye. |
| str_pad(str,newlength,pad_string,pad_type) | Pads a string to a new length. str and newlength are required, pad_string will specify which string to pad with (default is whitespace), and pad_type will specify which side should be padded - STR_PAD_BOTH, STR_PAD_LEFT, or STR_PAD_RIGHT. Default is STR_PAD_RIGHT. | str_pad("Hello World!",14,"_",STR_PAD_BOTH);="_Hello World!_" |
| str_repeat(str,num) | Repeats a given string a specified number of times. num must be greater than or equal to 0. | str_repeat(".",3);="..." |
| str_replace(find,replace,str,count) | Replaces one substring with another in string str. count, an optional parameter, is a variable which will keep track of the number of replacements made. | str_replace("World","You","Hello World!");="Hello You!" |
| str_split(str,num) | Splits the string into an array, each element being num characters long. If num is not specified, 1 is the default. | str_split("ABCDEFGHIJKLMNOP",4);=array("ABCD","EFGH","IJKL","MNOP"); |
| str_word_count(str,return,char) | Counts the number of words. If return is specified, the following values will result in different forms of return:
| str_word_count("Hello & Good Morning, World!",2,"&");=array([0]=Hello,[6]=&,[8]=Good,[13]=Morning,[22]=World) |
| strcmp(str1,str2) | Compares two strings (case sensitive). Returns 0 if equal, a number less than 0 if str1 is less than str2, and a number greater than 0 if str1 is greater than str2. | strcmp("Hello","HellO");=1 |
| strcasecmp(str1,str2) | Compares two strings (case insensitive). Returns 0 if equal, a number less than 0 if str1 is less than str2, and a number greater than 0 if str1 is greater than str2. | strcmp("Hello","HellO");=0 |
| strlen(str) | Returns the length of a string. | strlen("ABC");=3 |
| strpos(str,find,start) | Returns the first position of a substring inside a string, starting at position start. If start isn't specified, 0 is the default value. -1 returned if substring not found in string. | strpos("Hello World!","World");=6 |
| strrev(str) | Reverses a string | strrev("Hello!");="!olleH" |
| strtolower(str) | Converts a string to all lowercase letters. | strtolower("HELlo world.");="hello world." |
| strtoupper(str) | Converts a string to all uppercase letters. | strtoupper("HELlo world.");="HELLO WORLD." |
| substr(str,start,length) | Returns a substring of str, starting at position start and length in length. If length isn't specified, the default is to the end of the string. | substr("Hello World!",3,4)="lo W" |
This is by no means a complete list. For the complete Strings reference, see the PHP manual at www.php.net.
Comment on this page:
Comments
No one has commented on this lesson yet. You can be the first!