strchr() : finding substrings and extracting parts of strings

Find a substring with strchr()

strchr() is an alias of strstr(). It returns the portion of the string starting at the first matching substring, or false when no match is found.

$email = 'name@example.com';
echo strchr($email, '@'); // @example.com

strchr() is an alias of strstr(); both return the substring from the first case-sensitive match.

strchr(string $haystack, mixed $needle, bool $before_needle = false): string|false
Returns the substring starting from the match point onwards, or returns FALSE if no match is found in the string.

strchr() function in PHP performs a case-sensitive search to find the first occurrence of a specified character in a string. If you need a case-insensitive search, you might use stristr() instead.

Example 1: Basic Usage

Find the first occurrence of "world" in the string.
$text = "Hello world, the world is great!";
$result = strchr($text, "world");
echo $result;
world, the world is great!

Example 2: Using the before_needle Parameter

Return part of the string before the first occurrence of "world".
$text = "Hello world, the world is great!";
$result = strchr($text, "world", true);
echo $result;
Output
Hello 

Example 3: Case-Sensitive Search

Searching for "World" will not match "world" due to case sensitivity.
$text = "Hello world, the world is great!";
$result = strchr($text, "World");
echo $result ? 'True':'False';
Output
False

String Functions strstr(): Find the first substring


Subscribe to our YouTube Channel here



plus2net.com











PHP video Tutorials
✖
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
© 2000-2026 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer