Common PHP String Functions and Usage Examples
This tutorial introduces PHP's most commonly used string functions, categorizes them by purpose, and provides clear example code demonstrating how to retrieve string information, process, and convert strings, helping developers improve efficiency and code quality.
PHP provides a rich set of string functions that help developers manipulate text efficiently. This tutorial introduces the most commonly used PHP string functions, grouped by purpose, and demonstrates their usage with clear code examples.
Getting String Information
strlen(): Get the length of a string substr(): Extract a portion of a string strstr(): Find a substring strchr(): Find the last occurrence of a character in a string
Processing Strings
str_replace(): Replace characters within a string trim(): Remove whitespace from the beginning and end of a string str_pad(): Pad a string to a certain length str_split(): Split a string into an array
Converting Strings
strtolower(): Convert a string to lowercase strtoupper(): Convert a string to uppercase ucfirst(): Capitalize the first character of a string lcfirst(): Lowercase the first character of a string
Example Code:
// Get string length
$str = "Hello, world!";
$length = strlen($str);
echo "The length of the string is $length characters.";
// Extract substring
$str = "Hello, world!";
$sub_str = substr($str, 0, 5);
echo $sub_str; // Hello
// Find substring
$str = "Hello, world!";
$pos = strpos($str, ",");
echo $pos; // 5
// Find last occurrence
$str = "Hello, world!";
$pos = strrpos($str, ",");
echo $pos; // 10
// Replace characters
$str = "Hello, world!";
$str = str_replace("world", "PHP", $str);
echo $str; // Hello, PHP!
// Trim whitespace
$str = " Hello, world! ";
$str = trim($str);
echo $str; // Hello, world!
// Pad string
$str = "Hello, world!";
$str = str_pad($str, 20, "-");
echo $str; // -----Hello, world!-----
// Split string into array
$str = "Hello, world!";
$arr = str_split($str, 5);
print_r($arr); // Array ( [0] => Hello [1] => world! )
// Convert to lowercase
$str = "Hello, world!";
$str = strtolower($str);
echo $str; // hello, world!
// Convert to uppercase
$str = "Hello, world!";
$str = strtoupper($str);
echo $str; // HELLO, WORLD!
// Capitalize first letter
$str = "hello, world!";
$str = ucfirst($str);
echo $str; // Hello, world!
// Lowercase first letter
$str = "HELLO, WORLD!";
$str = lcfirst($str);
echo $str; // hello, world!Conclusion
PHP's string functions enable developers to handle text quickly and effectively; mastering them can improve development efficiency and code quality.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
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.
