Home >Backend Development >PHP Problem >How to use strstr() function in PHP
PHP
String operations are particularly common, and there are many ways to operate them. This article will take you through how to use PHP The
strstr() function in
finds the first occurrence of a string.
First, let’s take a look at the syntax of the strstr()
function.
strstr ( string $haystack , mixed $needle ,bool $before_needle = false )
$haystack: Input string.
$needle: If $needle is not a string, it will be converted to an integer and used as the character sequence number.
$before_needle: If true, strstr() will return the part of $needle before its position in $haystack.
Return value: Returns part of the string or false (if needle is not found).
Code example:
1. Only two parameters
<?php $email = '124455555@gamil.com'; echo $email."<br>"; $domain = strstr($email, '@'); echo $domain; ?>
输出:124455555@gamil.com @gamil.com
2. The third parameter is true
<?php $email = '124455555@gamil.com'; echo $email."<br>"; $domain = strstr($email, '@'); echo $domain; // 打印 @example.com echo "<br>"; $user = strstr($email, '@', true); // 从 PHP 5.3.0 起 echo $user; // 打印 name ?>
输出:124455555@gamil.com @gamil.com 124455555
Recommended: 《2021 PHP Interview Questions Summary (Collection)》《php video tutorial》
The above is the detailed content of How to use strstr() function in PHP. For more information, please follow other related articles on the PHP Chinese website!