12/14/07

PHP Strings Common PHP String Functions Substring Examples

PHP String

A string is an important data type in any programming language, used to manipulate informative data using different methods. PHP strings can be of any length, since there is no limits to the size of strings in PHP. The PHP strings can be specified using either single quote (ex. 'This is test string') or using double quotes (ex. "test string"). As compared with double quoted strings in single quoted PHP string variable will not be parsed and such string have only one escape character, single quote. Like any other programming languages, PHP also has some in built string manipulation functions. These PHP functions can directly be used in our code or other powerful functions can be derived using these basic string functionalities. Following is the list of some commonly used PHP functions along with examples.

Finding length of a PHP string using strlen

The strlen PHP string function is used to find the length of the given string. for example
$str = 'test string'; 
$len = strlen('$str');
echo $len; // this will output 11

Checking substring existence using strpos

PHP string function strpos can be used to check for substring existence. See example below:
$str = 'test string';
$sub_str = 'str';
if (strpos($str, $sub_str) === false) 
 echo "Substring not found";
else
 echo "Substring exist in given string";
Note the use of === operator because == will not work as expected since the return position can be 0 (start of string).
The offset value can be used here to specify from where to start searching. see example below:
$str = 'abcdefabcdef';
echo strpos($str, 'a', 1); // will print 6 not 0

Finding occurrence of a string

The strstr PHP string function is used to find the first occurrence. See below
$email = 'user@domain.com';
$domain = strstr($email, '@');
echo $domain; // this will print '@domain.com'
So strstr will return part of the string, starting from the given substring. If given substring does not exist, it will return FALSE. This string function is case-sensitive. For case-insensitive searches stristr is used , see below:
$domain = stristr('USER@EXAMPLE.com', 'e');
echo $domain; // This will print ER@EXAMPLE.com
Function strrchr(string str, char chr) is used to find the last occurrence of a character. This function returns the portion of string which starts at the last occurrence of given character and goes up to the end. see example below:
echo strrchr("abcdexyz", "e"); // will print exyz
echo substr(strrchr("module.filename.html", "."),1); // will print html

Return part of a string: Substring

The PHP function substr( string string, int start [, int length]) can be used to return part of a given string specified by start and length parameters. see examples below:
echo substr("abcdef", 1);    // will output "bcdef" , start 1 to end.
echo substr("abcdef", 1, 3); // will output "bcd", 1 to 3
echo substr("abcdef", 0, 4); // will output "abcd"
echo substr("abcdef", 0, 8); // will output "abcdef"
Using curly braces we can also return a character at given position. For example
$string = 'abcdef';
echo $string{0}; // will print a
echo $string{3}; // will print d
We can also use negative value of start parameter to return part from the end of string. see examples below:
substr("abcdef", -1);    // returns "f"
substr("abcdef", -2);    // returns "ef"
substr("abcdef", -3, 1); // returns "d"
Also negative value of length parameter can be used. In this case it will omit that many characters from the end of the string. See below:
echo substr("abcdef", 0, -1);  // will omit 1 char from the end and returns "abcde"
echo substr("abcdef", 2, -1);  // returns "cde"
echo substr("abcdef", 4, -4);  // returns ""
echo substr("abcdef", -3, -1); // returns "de"

5 comments:

Anonymous said...

Also an important and useful string functions are ucfirst and ucwords. The ucfirst function converts the first character of a given string to upper case. for example.
echo ucfirst("string functions");
will display String functions.

And ucwords will convert the first character of each word in a given string to uppercase. for example
echo ucwords("PHP string function");
will display PHP String Function.

Anonymous said...

Good post. It helped me a lot. Thank you!

Krrish said...

Useful information... Some of the points were really new to me.

Anonymous said...

Very Useful...THANK YOU...

Anonymous said...

echo substr("abcdef", 0, -1); // will omit 1 char from the end and returns "abcde"
echo substr("abcdef", 2, -1); // returns "cde"
echo substr("abcdef", 4, -4); // returns ""
echo substr("abcdef", -3, -1); // returns "de"