PHP Tutorial - PHP strpbrk() Function






Definition

The strpbrk() function searches a string for any of the specified characters.

This function is case-sensitive.

Syntax

PHP strpbrk() Function has the following syntax.

strpbrk(string,charlist)

Parameter

ParameterIs RequiredDescription
stringRequired.String to search
charlistRequired.Characters to find

Return

PHP strpbrk() Function returns the string starting from the character found, otherwise it returns FALSE.





Example 1

Search a string for the characters "oe", and return the rest of the string from where it found the first occurrence of the specified characters:


<?php
echo strpbrk("Hello world from java2s.com!","oe");
?>

The code above generates the following result.

Example 2

Search illegal characters in user name


<?php//w  w w.java  2s  .  c  o m
$myString = "Hello, world from java2s.com!"; 
echo strpbrk( $myString, "abcdef" ); // Displays 'ello, world!' 
echo strpbrk( $myString, "xyz" ); // Displays '' (false) 

$username = "php@example.com"; 
if ( strpbrk( $username, "@!" ) ) {
   echo "@ and ! are not allowed in usernames";     
}
?>

The code above generates the following result.





Example 3

This function is case-sensitive ("W" and "w" will not output the same):


<?php
echo strpbrk("Hello world from java2s.com!","W");
echo "<br>";
echo strpbrk("Hello world!","w");
?>

The code above generates the following result.