Common PHP String Functions and Usage Tips
This article introduces essential PHP string functions—including length, substring, search, replace, trimming, case conversion, splitting, joining, regular expressions, and comparison—explaining their syntax, providing code examples, and showing practical scenarios for efficient string manipulation in backend development.
PHP, a widely used server‑side scripting language, offers a rich set of string functions for handling and manipulating text. This article presents practical tips for using these functions to perform operations such as searching, replacing, extracting, and formatting strings.
String Length and Substring
In PHP, the strlen function returns the length of a string, while substr extracts a portion of the string. For example:
<code>$str = "Hello, world!";
$length = strlen($str); // $length is 13
$substring = substr($str, 0, 5); // $substring is "Hello"
</code>These functions are useful for limiting user input length or displaying specific parts of a string.
String Search and Replace
PHP provides strpos to locate the position of a substring and str_replace to replace occurrences. For example:
<code>$str = "Hello, world!";
$pos = strpos($str, "world"); // $pos is 7
$newStr = str_replace("world", "PHP", $str); // $newStr is "Hello, PHP!"
</code>These functions are commonly used to search for keywords in user input or replace specific text.
String Formatting
PHP includes functions such as trim , strtoupper , and strtolower for formatting strings. For example:
<code>$str = " Hello, World! ";
$trimmed = trim($str); // $trimmed is "Hello, World!"
$upper = strtoupper($str); // $upper is "HELLO, WORLD!"
$lower = strtolower($str); // $lower is "hello, world!"
</code>These functions help clean whitespace and adjust character case.
String Splitting and Joining
The explode function splits a string into an array, while implode joins array elements into a string. For example:
<code>$str = "apple,banana,orange";
$arr = explode(",", $str); // $arr is ["apple", "banana", "orange"]
$newStr = implode("-", $arr); // $newStr is "apple-banana-orange"
</code>These functions are handy for handling comma‑separated values or database fields.
Regular Expression Matching
The preg_match and preg_replace functions enable pattern‑based searching and replacement. For example:
<code>$str = "The quick brown fox jumps over the lazy dog";
if (preg_match("/fox/", $str)) {
echo "Found the fox!";
}
$newStr = preg_replace("/brown/", "red", $str); // $newStr is "The quick red fox jumps over the lazy dog"
</code>Regular expressions provide flexible and powerful string manipulation.
String Comparison and Sorting
PHP offers strcmp and strcasecmp for comparing strings, and asort and ksort for sorting arrays. For example:
<code>$str1 = "apple";
$str2 = "banana";
if (strcmp($str1, $str2) < 0) {
echo "$str1 is before $str2";
}
$arr = ["banana", "apple", "orange"];
asort($arr); // $arr becomes ["apple", "banana", "orange"]
</code>These functions are essential for ordering and comparing textual data.
By mastering these PHP string functions—from length and extraction to search, replace, formatting, splitting, regular expressions, and comparison—developers can handle text more efficiently and flexibly in backend applications.
php中文网 Courses
php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.