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

The PHP strstr() function searches for the first occurrence of a needle in a haystack string and returns either the remainder of the haystack or, optionally, the portion before the needle, with detailed parameter descriptions and example code illustrating its use.

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

strstr() is a PHP function that searches a string for the first occurrence of a needle and returns the portion of the haystack from that point to the end (or optionally the part before the needle).

Parameters

haystack – the input string to search.

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

before_needle – optional boolean; when true the function returns the part of haystack before the needle.

Return value

Returns the resulting substring on success, or FALSE if the needle is not found.

Example

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

$user = strstr($email, '@', true);
echo $user; // prints name
?>
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.

PHPfunctionstring-manipulationstrstr
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.