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:

FunctionUsageExample
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!");
implode(" ",$arr);
="Hello World!";
ltrim(str)Removes leading whitespace from a stringltrim(" PHP");="PHP"
rtrim(str)Removes trailing whitespace from a stringrtrim("PHP ");="PHP"
trim(str)Removes leading and trailing whitespace from a stringtrim(" 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:
  • 0 - Returns the number of words found (default)
  • 1 - Returns an array with the words from the string
  • 2 - Returns an associative array where the key is the position of the word in the string, and the value is the actual word.
If char is specified, special characters can be considered words on their own.
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 stringstrrev("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.

<<PreviousNext>>

Comment on this page:

Your information Name:
Email:
Hide Email?
Enter the 5 characters shown.
Request another image.
Prove your human:
Comments Overall page rating:

Comments

No one has commented on this lesson yet. You can be the first!