PHP strpbrk() Function: Find Characters in a String

This article explains the PHP strpbrk() function, which searches a haystack string for any character from a given list, details its parameters and return value, and provides two illustrative code examples demonstrating case‑sensitive matching and character selection.

Laravel Tech Community
Laravel Tech Community
Laravel Tech Community
PHP strpbrk() Function: Find Characters in a String

The PHP function strpbrk() searches a haystack string for the first occurrence of any character from a specified char_list.

Signature: string strpbrk(string $haystack, string $char_list) It returns the portion of $haystack starting from the first matched character; if no character from $char_list is found, it returns FALSE.

Parameters $haystack: The string to be searched. $char_list: A string containing the characters to search for (case‑sensitive).

Return value

A substring of $haystack beginning with the first matched character, or FALSE if no match is found.

Example 1

```php php $text = 'This is a Simple text.'; // Output "is is a Simple text.", because 'i' is matched first echo strpbrk($text, 'mi'); // Output "Simple text.", because the search is case‑sensitive echo strpbrk($text, 'S'); ? ```

In the first call, the function finds the character i (from 'mi') and returns the substring starting at that position. In the second call, only an uppercase S matches, so the returned substring starts at S.

Example 2

```php php echo strpbrk("I love Shanghai!", "S"); // Outputs "Shanghai!" ? ```

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

BackendPHPCode Examplestrpbrk
Laravel Tech Community
Written by

Laravel Tech Community

Specializing in Laravel development, we continuously publish fresh content and grow alongside the elegant, stable Laravel framework.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.