Most common PHP string manipulation questions
Alula TYC
Posted on October 12, 2021
These are the most common PHP string manipulation questions that I had come around when I was getting started with PHP. Most of these easy concepts still confuse students. But let’s take few minutes and take them one by one.
How do I combine two strings together?
The first common PHP string manipulation question is “How do I combine two strings?”. Yes, it is very common.
The reason is confusion between PHP concatenation operators and other languages’. For instance, in JavaScript concatenation is done using the plus sign (+). So, if you experience this confusion, don’t fret.
Obviously, for a person with basic knowledge and experience, the answer is simple. We use the dot sign (.) operator for concatenation in PHP. Here is a simple snippet.
$var1 = "Learn";
$var2 = "PHP";
$message = $var1 . ' '.$var2;
But, I am sure we can also think of a couple of more ways to answer this common PHP string manipulation question. For instance, we can use string interpolation with curly braces to combine the two string variables.
$var1 = "Learn";
$var2 = "PHP";
$message = "{$var1} {$var2}";
Normally, we don’t need curly braces when interpolating strings. But, we need them in this scenario. Since we are performing interpolation and concatenation. Beware that this works with double quotations string only.
Finally, we can also use printf and echo to combine strings and print them.
How to check if a string contains another word in PHP?
Next, checking if a string contains another string seems a very important utility that PHP didn’t provide until recently (The release of version 8). Yet, until now there were a number of ways of answer this common PHP string manipulation question and we will see them all.
Okay, let’s see how we will answer this question one by one. Firstly, the simple & go-to solution is to use strpos() function.
Using strpos() funciton
This method works fine as long as we use identity o This method works fine as long as we use the identity operator instead of equality operators. Because the function, strpos(), returns 0 when the second string is located as the first character or string. And, PHP will cast 0 as FALSE during comparison operators.
$var1 = "Programming with PHP is Awesome.";
$var2 = "PHP";
if(strpos($var1, $var2) !== FALSE) {
echo "There is $var2 in $var1";
}
Using substr_count() PHP function
We can use the substr_count() PHP string function to count the number of occurrences of one string in another string. That means we can check if the number of occurrences is greater than 0 and then be able to answer our PHP string manipulation question. Here is an example:-
$var1 = "Programming with PHP is Awesome.";
$var2 = "PHP";
if(substr_count($var1, $var2) > 0) {
echo "There is $var2 in $var1";
}
Using strstr() function
Similar to the strpos() function and substr_count, PHP has strstr() string utility function. And this function simply returns the first occurrence of a string in another string. Hence, our job is almost made very easy.
$var1 = "Programming with PHP is Awesome.";
$var2 = "PHP";
if(strstr($var1, $var2) !== FALSE) {
echo "There is $var2 in $var1";
}
Using regular expression
In addition to all the above methods, we can also use the ubiquitous regex function called preg_match to see if a string contains another string. In fact, we can always use RegEx to answer the most common PHP string manipulation questions. For example:
$var1 = "Programming with PHP is Awesome.";
$var2 = "PHP";
if(preg_match('/.$var2./', $var1) !== FALSE) {
echo "There is $var2 in $var1";
}
Using str_contains() PHP 8 function
Finally, PHP 8 gives us the most direct approach. To check if one string contains another. We can use str_contains PHP 8 function, which does exactly what it says. That is, it checks if a string contains another string.
$var1 = "Programming with PHP is Awesome.";
$var2 = "PHP";
if(str_contains($var1, $str2) > 0) {
echo "There is $var2 in $var1";
}
Posted on October 12, 2021
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.