📖 PHP String Functions

Here are some additional string functions that may be useful for data modification.

String Function Examples
FunctionDescriptionFormat
split() breaks a string into an array based on a delimiter
$arrayName = split("delimiter", $stringName);

Example

$phrase = "Out to lunch";
$words = split(" ", $phrase);
// $words would now be an array with the following values:
// $words[0] = "Out ", $words[1] = "to ", $words[2] = "lunch"
strtok() creates a substring using a predetermined separator
$varName = strtok($stringName, "delimiter");

Example

$phrase = "Out to lunch";
$firstWord = strtok($phrase, ' ');
// $firstWord would = "Out"
str_word_count() returns the number of words in a string
$varName = str_word_count($stringName);

Example

$phrase = "Out to lunch";
$wordCnt = str_word_count($phrase');
// $wordCnt would = 3
str_ireplace() replaces a substring with a new value
$varName = str_ireplace("oldValue", "newValue", $stringName);

Example

$phrase = "Out to lunch";
$phrase = str_ireplace("lunch", "dinner", $phrase);
// $phrase would = "Out to dinner"
rtrim()
ltrim()
trim()
removes whitespace from the end of a string,
beginning of a string or both ends of a string
$varName = rtrim($varName);

Example

$words[0] = rtrim($words[0]);
// $words[0] now contains "Out"
strlen() retrieves the number of characters in the string
$length = strlen($varName);

Example

$room = "Room P109";
$length = str($room);
// $length would contain 9.
substr() retrieves a part of a string
$varName = substr($origVarName, startPosition, length)

Example

$roomNumber = "Room P109";
$numOnly = substr($roomNumber, 5, 4);
/* or */
$numOnly = substr($roomNumber, 5, strlen($roomNumber ) - 1);
// $numOnly would contain "P109"
strstr() searches for a string within a string
strstr(stringToSearchIn, stringToSearchFor)

Example

$course = array(
			"Web Design I",
			"Web Design II",
			"PHP Programming",
			"Intro to MySQL",
			"Beginning Web Programming"
			);
foreach ($course as $webCourse) {
	if (strstr($webCourse, "Web")) {
		print "$webCourse
"; } } // This would print on the page: Web Design I Web Design II Beginning Web Programming
strcmp() Compares two strings (case-sensitive)
result = strcmp(firstString, secondString)
// returns 0 if they match,
// 0 if the second string is smaller

Example

$string1 = "Hello, world!";
$string2 = "Hello, World!";
$same = strcmp($string1, $string2);
if ($same == 0) {
	print "They are the same";
} else {
	print "They are different";
}
// This would print "They are different" because of capitalization.
strcasecmp() Compares two strings (case-insensitive)
result = strcmp(firstString, secondString)
// returns 0 if they match,
// 0 if the second string is smaller

Example

$string1 = "Hello, world!";
$string2 = "Hello, World!";
$same = strcmp($string1, $string2);
if ($same = 0) {
	print "They are the same";
} else {
	print "They are different";
}
// This would print "They are the same" because it is case insensitive.
strtolower() converts a string to lower case
result = strtolower(string)

Example

$string1 = "Hello, World!";
$ string2 = strtolower($string1);
// $string2 would contain "hello, world!"
strtoupper() converts a string to upper case
result = strtoupper(string)

Example

$string1 = "Hello, World!";
$string2 = strtoupper($string1);
// $string2 would contain "HELLO, WORLD!"
ucfirst() converts the first letter in a string to upper case
result = ucfirst(string)

Example

$string1 = "hello, World!";
$string2 = ucfirst($string1);
// $string2 would contain "Hello, World!"
// usfirst does not affect any nonalphabetical characters
// or any other capitalized letters in the string
ucwords() converts the first letter in each word in a string to upper case
result = ucfirst(string)

Example

$string1 = "hello, world!";
$string2 = ucfirst($string1);
// $string2 would contain "Hello, World!"