Working with String Data Type in PHP
Strings are very common in PHP, so it's vital to learn how to handle them effectively. Fortunately, PHP provides many functions for dealing with Strings. Once you understand the purpose of a few PHP functions, you can easily handle a variety of Strings, from simple to complex.
Common String Handling Functions:
1. strlen()
The strlen() function is used to return the length of a String.
echo strlen("Hello world!"); // Output: 12
2. str_word_count()
The str_word_count() function is used to count the number of words in a String.
echo str_word_count("Hello world!"); // Output: 2
3. strrev()
The strrev() function is used to reverse a String.
echo strrev("Hello world!"); // Output: !dlrow olleH
4. strpos()
The strpos() function is used to search for a specific text within a String. If a match is found, it returns the position of the first character of the match. If no match is found, it returns FALSE.
// Search for the text "world" in the String "Hello world!":
echo strpos("Hello world!", "world"); // Output: 6
Note: The first character's position in a string is 0 (not 1).
5. str_replace()
The str_replace() function is used to replace some characters with some other characters in a string.
// Thay thế "world" thành "Dolly"
echo str_replace("world", "Dolly", "Hello world!"); // Output: Hello Dolly!
There are many more functions for handling Strings, refer more here: STRING HANDLING FUNCTION DOCUMENTATION
Submit feedback
Your email address will not be made public. Fields marked are required *
Search
Trend
-
What is Black Myth: Wukong? Detailed Guide on System Requirements and Gameplay
08-21-2024 . 870 view
-
Call of Duty: Black Ops 6 - Intense, Mysterious, and Surprising Warfare
09-02-2024 . 745 view
-
The "End of Life" for Windows 10: A Massive E-Waste Threat and Sustainable Solutions
08-18-2024 . 725 view
-
Casio WS-B1000: The Perfect Blend of Traditional Watch and Smartwatch
08-11-2024 . 641 view
-
Lost All Money by Trusting in "Elon Musk Deepfake": A Cautionary Tale of Deepfake
08-21-2024 . 609 view
0 feedback