PHP strstr() Function: Usage, Parameters, Return Values, and Examples

This article explains the PHP strstr() function, detailing its signature, parameters, return values, and providing clear code examples that demonstrate how to extract substrings before or after a specified needle within a string.

Laravel Tech Community
Laravel Tech Community
Laravel Tech Community
PHP strstr() Function: Usage, Parameters, Return Values, and Examples

strstr() searches for the first occurrence of a substring within a string and can return either the portion of the string from that point onward or the part before it.

Signature:

string strstr(string $haystack, mixed $needle, bool $before_needle = false)

Parameters:

haystack : the input string.

needle : the substring to search for; if not a string it is converted to an integer and used as a character position.

before_needle : if true, returns the part of $haystack before $needle.

Return value: the portion of the string as described, or FALSE if the needle is not found.

Example 1:

<?php
$email = '[email protected]';
$domain = strstr($email, '@');
echo $domain; // prints @example.com
$user = strstr($email, '@', true);
echo $user; // prints name
?>

Example 2:

<?php
echo strstr("Hello world!", "world", true); // prints Hello
?>
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.

Backendprogrammingstring-functionsphp-strstr
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.