The PHP strpbrk is an inbuilt function in PHP. It searches a string for a set of characters and returns the section of the string from which it found any one of the characters. However, if any character does not exist in the function, it returns false. Also, the function performs a case-sensitive search. Therefore, it treats uppercase and lowercase characters differently.
In this article, we will discuss the PHP strpbrk Function. Also, we will discuss a few examples of using it.
strpbrk ($string, $charList);
The PHP strpbrk Function expects two parameters. Also, both parameters are mandatory. The description of the parameters is as below:
- $string: The string in which you want to find characters. Also, it is a mandatory parameter.
- $charList: The second parameter defines a list of characters to find. It is also a mandatory parameter.
The PHP strpbrk Function returns the remaining string from the first occurrence of any one of the character. However, if no character exists in the string, it returns false.
Let’s discuss a few examples of using the function.
Example 1: Searching a String
For instance, consider a simple string to search.
<?php $string = 'Concatly, Brining Knowledge Together'; $charList = 'ln'; echo strpbrk($string, $charList); ?>
In the above example, we search for characters ‘ln’ in the input string. The character n appears first in the string. Therefore, the function returns the remaining string after the first found character.
OUTPUT: ncatly, Brining Knowledge Together
Example 2: Case-Sensitive Search
Also, the function performs a case-sensitive search. We can observe this in the following example:
<?php $string = 'Concatly, Brining Knowledge Together'; $charList = 'L'; var_dump(strpbrk($string, $charList)); ?>
Since, the string consists of lowercase l and not uppercase L, the function returns false.
Vishesh is currently working as a Lead Software Engineer at Naukri.com. He passed out of Delhi College of Engineering in 2016 and likes to play Foosball. He loves traveling and is an exercise freak. His expertise includes Java, PHP, Python, Databases, Design and Architecture.