PHP, a popular server-side scripting language, offers a rich library of built-in string functions. These functions allow developers to manipulate, search, and format text efficiently. In this article, we’ll explore the most commonly used PHP string functions and learn how to use them effectively.

String Length: strlen()

The strlen() function returns the length of a given string.

$string = "Hello, world!";
$length = strlen($string);
echo $length; // Output: 13

Substring: substr()

substr() returns a portion of a string, specified by the start index and length.

$string = "Hello, world!";
$substring = substr($string, 0, 5);
echo $substring; // Output: "Hello"

String Position: strpos(), stripos(), strrpos(), strripos()

These functions find the position of a substring in a string.

  • strpos(): Case-sensitive, first occurrence
  • stripos(): Case-insensitive, first occurrence
  • strrpos(): Case-sensitive, last occurrence
  • strripos(): Case-insensitive, last occurrence
$string = "Hello, world!";
$position = strpos($string, "world");
echo $position; // Output: 7

String Replace: str_replace(), str_ireplace()

These functions replace a specified substring with another substring.

  • str_replace(): Case-sensitive
  • str_ireplace(): Case-insensitive
$string = "Hello, world!";
$replaced = str_replace("world", "PHP", $string);
echo $replaced; // Output: "Hello, PHP!"

Case Conversion: strtolower(), strtoupper(), ucfirst(), lcfirst(), ucwords()

These functions convert the case of characters in a string.

  • strtolower(): Convert to lowercase
  • strtoupper(): Convert to uppercase
  • ucfirst(): Uppercase the first character
  • lcfirst(): Lowercase the first character
  • ucwords(): Uppercase the first character of each word
$string = "Hello, world!";
$uppercase = strtoupper($string);
echo $uppercase; // Output: "HELLO, WORLD!"

String Comparison: strcmp(), strcasecmp(), strnatcmp(), strnatcasecmp(), strcoll()

These functions compare two strings.

  • strcmp(): Case-sensitive
  • strcasecmp(): Case-insensitive
  • strnatcmp(): Case-sensitive, natural order
  • strnatcasecmp(): Case-insensitive, natural order
  • strcoll(): Locale-based, case-sensitive
$result = strcmp("apple", "banana");
echo $result; // Output: -1 (negative if str1 is less than str2)

String Split: explode(), implode()

explode() breaks a string into an array based on a delimiter, while implode() joins array elements into a string using a delimiter.

  • explode(): Split string into an array
  • implode(): Join array elements into a string
$string = "Hello, world!";
$array = explode(", ", $string);
print_r($array); // Output: Array ( [0] => Hello [1] => world! )

$joined = implode(" - ", $array);
echo $joined; // Output: "Hello - world!"

String Trimming: trim(), ltrim(), rtrim()

These functions remove whitespace or other specified characters from the start and/or end of a string.

  • trim(): Remove characters from both sides
  • ltrim(): Remove characters from the left side
  • rtrim(): Remove characters from the right side
$string = "   Hello, world!   ";
$trimmed = trim($string);
echo $trimmed; // Output: "Hello, world!"

Formatting Strings: printf(), sprintf(), vprintf(), vsprintf()

These functions format strings with placeholders and values.

  • printf(): Output a formatted string
  • sprintf(): Return a formatted string
  • vprintf(): Output a formatted string with an array of arguments
  • vsprintf(): Return a formatted string with an array of arguments
$number = 42;
$format = "The answer is %d.";
printf($format, $number); // Output: "The answer is 42."

Regular Expressions: preg_match(), preg_match_all(), preg_replace(), preg_replace_callback(), preg_split()

These functions work with regular expressions to match, replace, and split strings.

  • preg_match(): Match a pattern once
  • preg_match_all(): Match a pattern multiple times
  • preg_replace(): Replace a pattern with a string
  • preg_replace_callback(): Replace a pattern with a callback function
  • preg_split(): Split a string based on a pattern
$string = "Hello, world!";
$pattern = "/world/";
$replacement = "PHP";

$result = preg_replace($pattern, $replacement, $string);
echo $result; // Output: "Hello, PHP!"

In this article, we’ve covered a wide range of string functions in PHP. By mastering these functions, you’ll be able to effectively manipulate, search, and format text in your PHP projects.