Current Location: Home> Latest Articles> Common PHP String Functions Explained with Example Code

Common PHP String Functions Explained with Example Code

gitbox 2025-06-30

Overview of PHP String Functions

PHP is a widely-used server-side scripting language, particularly popular in web development. String manipulation is one of the most common operations in PHP. Mastering some essential string functions can significantly improve development efficiency. In this article, we will introduce some commonly used PHP string functions and demonstrate their usage with example code.

strlen()

The strlen() function is used to get the length of a string, returning the number of characters in the string.


$str = "Hello, world!";
$length = strlen($str);
echo "String length: " . $length;

This code will output:

String length: 13

The strlen() function is useful for validating input and counting the number of characters in a string.

strpos()

The strpos() function is used to find the position of the first occurrence of a substring within a string.


$str = "Hello, world!";
$position = strpos($str, "world");
echo "Position of \"world\" in the string: " . $position;

This code will output:

Position of "world" in the string: 7

The strpos() function is commonly used to search for specific words, variables, or tokens within a string.

str_replace()

The str_replace() function replaces characters or substrings in a string with other characters or substrings.


$str = "Hello, world!";
$newStr = str_replace("world", "PHP", $str);
echo $newStr;

This code will output:

Hello, PHP!

The str_replace() function is useful for dynamically replacing text or filtering user input in real-time.

strtolower() and strtoupper()

The strtolower() function converts a string to lowercase, while the strtoupper() function converts it to uppercase.


$str = "Hello, world!";
$lower = strtolower($str);
$upper = strtoupper($str);
echo "Converted to lowercase: " . $lower;
echo "Converted to uppercase: " . $upper;

This code will output:

Converted to lowercase: hello, world!

Converted to uppercase: HELLO, WORLD!

These functions are useful when comparing strings while ignoring case differences.

substr()

The substr() function extracts a part of a string. It takes a starting position and an optional length parameter and returns the substring of the specified length.


$str = "Hello, world!";
$subStr = substr($str, 7, 5);
echo "Substring: " . $subStr;

This code will output:

Substring: world

The substr() function is often used to extract file names from URLs or extract a portion of a string from a larger text.

Conclusion

This article has summarized some of the most commonly used PHP string functions, including strlen(), strpos(), str_replace(), strtolower(), strtoupper(), and substr(). These functions are incredibly useful for string manipulation and validation, allowing developers to handle strings efficiently.

By learning and practicing these essential PHP string functions, you will be able to manipulate strings more effectively, improving both the maintainability and efficiency of your code.