strchr() : finding substrings and extracting parts of strings
PHP String
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 chunk_split(): Split a string into smaller chunks →
← Subscribe to our YouTube Channel here
This article is written by plus2net.com team.
https://www.plus2net.com
plus2net.com