Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

PHP String strpbrk() Function



The PHP String strpbrk() function is used to search a string for the given characters. This function returns the remainder of the string from which it found the first occurrence of any of the given characters. If no characters are found, it returns false. This function is case sensitive.

Syntax

Below is the syntax of the PHP String strpbrk() function −

string strpbrk ( string $string, string $characters )

Parameters

Here are the parameters of the strpbrk() function −

  • $string − (Required) It is the string where characters is looked for.

  • $characters − (Required) It is the characters to find in the string.

Return Value

The strpbrk() function returns a string starting from the character found, or FALSE on not found.

PHP Version

First introduced in core PHP 5, the strpbrk() function continues to function easily in PHP 7, and PHP 8.

Example 1

Here is the basic example of the PHP String strpbrk() function to search the string for the given character.

<?php
   echo strpbrk("Tutorialspoint","p");
?>

Output

Here is the outcome of the following code −

point

Example 2

In the below PHP code we will use the strpbrk() function and check if the string contains any of the characters a, e, i, o, u.

<?php
   // Define string here
   $string = "hello world";
   $characters = "aeiou";

   $result = strpbrk($string, $characters);

   if ($result !== false) {
      echo "The first vowel in the string is: $result\n";
   } else {
      echo "No vowels found in the string.\n";
   }
?> 

Output

This will generate the below output −

The first vowel in the string is: ello world

Example 3

Now the below code uses the strpbrk() function to case-sensitive search for the characters H, E, L.

<?php
   // 
   $string = "hello world";
   $characters = "HEL";

   $result = strpbrk($string, $characters);

   if ($result !== false) {
      echo "Match found: $result\n";
   } else {
      echo "No match found for uppercase characters.\n";
   }
?> 

Output

This will create the below output −

No match found for uppercase characters.

Example 4

In the following example, we are using the strpbrk() function to search for digits in a mixed string.

<?php
   // Define string here
   $string = "Order1234: Your package will arrive soon.";
   $characters = "0123456789";

   $result = strpbrk($string, $characters);

   if ($result !== false) {
      echo "The first digit in the string is: $result\n";
   } else {
      echo "No digits found in the string.\n";
   }
?> 

Output

Following is the output of the above code −

The first digit in the string is: 1234: Your package will arrive soon.
php_function_reference.htm
Advertisements