Working with strings in PHP is almost as straight forwards as it gets. And to learn php string functions can be quick and easy. There are so many functions to manipulate them, that it can take only a few minutes to figure them all out. And yet, they are so powerful and quick to use.
What is a string and how to use them?
A string in is simply a collection of characters. Just a word or a sentence, wrapped in quotation marks and usually stored in a variable, e.g. $super_sweet_string = “super cool text”.
$my_first_string = "look there is some text here"; $my_second_string = "and here"; $another_string = $my_first_string . " " . $my_second_string;
A string variable is created like any other variable in PHP, using the $ prefix followed by the name of the variable, e.g. $my_first_string.You can assign pretty much any length of characters put together as the value of a string, you need only wrap them in either single or double quotation marks, e.g. “this is a string text”.
You can also assign a value to a string by combining, or appending multiple strings together. To do this you need only separate each string with a single period ( . ). In the example above I am combining both variables and hard set strings together. If we were to display the $another_string variable we would see “look there is some text here and here” (without quotation marks).
How to display strings?
echo "my super cool string"; $string_var = "another sweet string"; echo $string_var; // displays: "another sweet string"
The echo command is used to display a string; either a hard coded string like line 1 or a string variable such as in line 3. You can also append strings together and display them using the echo command.
Everyone uses different php string functions in their code. All depending on what they are trying to do. These are the best php string functions that I use. Most of them are a must for any web app:
Best PHP String Functions
- sub strings or string fragments: substr()
- string position and location: strpos()
- shuffle a string: str_shuffle()
- convert a string to lower case: strtolower()
1. Sub strings (aka string fragments)
The substr() function allows us to read only a specific portion of a string. It requires at least two parameters, but may take three. Parameter 1 (string type) is the original string you are starting with, parameter 2 (integer type) is the start position in original string, and parameter 3 (integer type; optional) is the length of the substring you are creating. The last parameter is not required and if you don’t provide it, then it will default to the remaining length of the string from the start position.
substr("my string", 3); // "string" substr("another string", 2, 9); // "other str" $var1 = "starting value"; $var2 = substr($var1, 5, 7); // "ing val"
In the examples above, we can see the substr() function in action. Notice that we can not only pass hard coded strings to the function, but also pass variables as well. Even store the results in a variable itself.
2. String position and location
The strpos() function allows us to find a specific occurrence of a character or string with an original string, find the needle in the haystack if you will. Parameter 1 (string type) is the haystack, parameter 2 (string type) is the needle, and parameter 3 (integer type; optional) is the start position for the search. This function will return an integer of the starting position of your needle. Say we had a string of “needle in the haystack” and we searched for “needle”, the function would return 0 because “needle” is 0 characters from the beginning of the string. If we searched for “hay”, we would get 10 because “hay” is 10 characters from the beginning of the string.
strpos("needle in the haystack", "hay"); // 14 $var1 = strpos("find this one", "one"); // 10 $var2 = strpos("the same word in the same sentence", "same", 10); // 21
3. Shuffle a string (jumble a string all up!)
The str_shuffle() function can be used to, as you might guess, shuffle a string. The only parameter it takes is the string you want to shuffle. Every time you use this function, you will get a different output string. Its great for creating random strings for users who need to reset their password or something to that effect.
$base = "abcdefgh"; $output = str_shuffle($base); // $output = "hbfaegdc"
4. Convert a string to lower case
The strtolower() functions takes the input string and makes every character lower case. The opposite is true for the strtoupper(), every character will be upper case.
strtolower("EVER NOTICE HOW SOME PEOPLE TYPE ALL CAPS??"); // "ever notice how some people type all caps??" strtouper("and maybe some need it every now and then"); // "AND SOME NEED IT EVERY NOW AND THEN"
4.1 Make the first letter of a string upper or lower case
If you want to make just the first letter of a string upper case, use ucfirst(). You can also make the first letter of every word in a string uppercase using the ucwords() function. The lcfirst() functions lets you make the first character of a string lower case. I’m not really sure when that is useful but hey, if you need it, it’s there.
$sentence = ucfirst("this is kinda cool"); // "This is kinda cool" $name = ucwords("nick frosty"); // "Nick Frosty" $totally_useful = lcfirst("USEFUL"); // "uSEFUL"
More on php string functions?
String formatting for SQL queries are a little different than regular strings. Checkout my article on database operations and MySQLi to learn more.
Obviously, this post doesn’t have an all-inclusive list of the available functions to manipulate strings, but it’s a good start for someone new to PHP. To learn more php basics, like strings, check out the PHP reference website to see a list of all of them, it will give you a more comprehensive list of what you can do with strings. There are some pretty cool options to!