How to use strstr() function in PHP

autoload
Release: 2023-03-09 12:06:02
Original
2033 people have browsed it

    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 )
Copy after login
  • $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; 
?>
Copy after login
输出:124455555@gamil.com
       @gamil.com
Copy after login

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
?>
Copy after login
输出:124455555@gamil.com
       @gamil.com
       124455555
Copy after login

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!

Related labels:
php
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!