Home >Backend Development >PHP Problem >How to use strstr() function in PHP

How to use strstr() function in PHP

autoload
autoloadOriginal
2021-04-20 11:51:572056browse

    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  = &#39;124455555@gamil.com&#39;;
echo $email."<br>";
$domain = strstr($email, &#39;@&#39;);
echo $domain; 
?>
输出:124455555@gamil.com
       @gamil.com

2. The third parameter is true

<?php
$email  = &#39;124455555@gamil.com&#39;;
echo $email."<br>";
$domain = strstr($email, &#39;@&#39;);
echo $domain; // 打印 @example.com
echo "<br>";
$user = strstr($email, &#39;@&#39;, 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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn