PHP strpbrk Function | Search String For Set of Characters

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.

Syntax

strpbrk ($string, $charList);

Parameters

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.

Return Value

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.

Examples

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.

OUTPUT:
bool(false)

Conclusion

In conclusion, we discussed the PHP strpbrk Function. You can read more about it on the Official PHP Documentation. Additionally, you can read PHP String Functions on Concatly.

Spread the Knowledge

Leave a Reply

Your email address will not be published. Required fields are marked *