Checking for a substring in a string is one of the most common tasks a developer does. In PHP, there’s more than one way to accomplish this task. Today, we will learn all the different ways to check if a string contains a substring in PHP.
Case Sensitive Matching
Below we will look at different ways to search for a substring that matches the exact casing in the given string.
Using Strpos()
You can use strpos()
to find the first occurrence of a substring in a string. This method is one of the most common methods used for finding a substring.
Usage
strpos ( string $haystack , mixed $needle [, int $offset = 0 ] ) : int
strpos()
accepts the following parameters:
- $haystack: the string that you will search in
- $needle: the substring that you’re searching for
- $offset (optional): defines the character index to begin from. If you pass a negative number, then it begins from the end of the string.
Example:
/**
* A method to find substring in a string
*
* @param String $stringToSearch This is the sub string you want to search
* @param String $searchString The is the string in which substring
* will be searched for
*
* @return boolean true if $stringToSearch exists
* in $searchString, false otherwise
*/
public function findString($stringToSearch, $searchString) {
if (strpos($stringToSearch, $searchString) !== false) {
return true;
} else {
return false;
}
}
In the above example, we made a custom method that uses strpos()
to search for a given substring in a string and returns a boolean value that determines whether a match was found. The method can be used like this:
echo $this->findString('code on codezen.io', 'code'); // output => true
echo $this->findString('code on codezen.io', 'sing'); // output => false
Using $offset
Here is an example of how you could use $offset
to exclude a certain number of characters from start or end in your search. In the following example, the search begins from the second character (or character index of 1):
<?php
// Here search for the character and ignore anything before the offset
$newstring = 'code on codezen.io';
$pos = strpos($newstring, 'code', 1); // $pos = 9, not 0
?>
Regex based matching using preg_match()
We can also use regular expressions to find a substring in a string. To do this, we make use of PHP’s preg_match()
method to perform a regular expression match against a string.
preg_match ( string $pattern , string $subject [, array &$matches [, int $flags = 0 [, int $offset = 0 ]]] ) : int
preg_match()
searches the given $subject
for a match to the regular expression given in $pattern
.
/**
* A method to find substring in a string using preg_match
*
* @param String $pattern This is the sub string you want to search
* @param String $searchString The is the string in which substring
* will be searched for
*
* @return boolean true if $stringToSearch exists
*/
public function findStringPattern($pattern, $searchString) {
if (preg_match('/\b'.$pattern.'\b/', $searchString)) {
return true;
} else {
return false;
}
}
The above method can be used like this:
echo $this->findStringPattern('code on codezen.io', 'code'); // output => true
echo $this->findStringPattern('code on codezen.io', 'sing'); // output => false
Case-Insensitive Matching
In the previous section, you learned how to do a case-sensitive search for a substring. In this section, we’ll learn how to do a case-insensitive search.
Use stripos() for case-insensitive matching
If you want to match a substring regardless of its case, you can use PHP’s stripos() method. This function works similarly to the strpos()
method that we discussed initially.
stripos()
accepts the following parameters:
- $haystack: the string that you will search in
- $needle: the substring that you’re searching for
- $offset (optional): defines the character index to begin from. If you pass a negative number, then it begins from the end of the string.
stripos ( string $haystack , mixed $needle [, int $offset = 0 ] ) : int
Here is an example of how you can use stripos()
in your application:
/**
* A method to find case insensitive substring in a string
*
* @param String $stringToSearch This is the sub string you want to search
* @param String $searchString The is the string in which substring
* will be searched for
*
* @return boolean true if $stringToSearch exists
* in $searchString, false otherwise
*/
public function findCaseInsensitiveString($stringToSearch, $searchString) {
if (stripos($stringToSearch, $searchString) !== false) {
return true;
} else {
return false;
}
}
In the above example, we made a custom made that uses stripos()
to search for a given substring in a string and returns a boolean value that determines whether a match was found. The method can be used like this:
echo $this->findCaseInsensitiveString('code on codezen.io', 'code'); // output => true
echo $this->findCaseInsensitiveString('code on codezen.io', 'Code'); // output => false
Find Number of Occurrences of a Substring
If you want to find out the number of times a substring appears in a given string, you can use the substr_count()
method.
substr_count ( string $haystack , string $needle [, int $offset = 0 [, int $length ]] ) : int
Similar to the methods discussed above you need to pass a string ($haystack
) and a substring ($needle
) to search. We will see an example usage below:
$searchString = "learn how to code with codezen.io";
$stringToSearch = "code";
$count = substr_count($searchString, $stringToSearch);
echo "The count is: " . $count;
Output
The count is: 2
Wrapping Up
These are some of the different ways to search for a particular word, phrase, or string within another string. If you have any questions or requests, feel free to drop a comment below.
Also, be sure to check out our PHP tutorials for interesting articles like this one.
Comments