search
HomeBackend DevelopmentPHP TutorialPHP字符串函数之 strstr stristr strchr strrchr

  • strstr -- 查找字符串的首次出现,返回字符串从第一次出现的位置开始到该字符串的结尾或开始。
  • stristr -- strstr 函数的忽略大小写版本
  • strchr -- strstr 函数的别名
  • strrchr -- 查找字符串的最后一次出现,返回字符串从最后一次出现的位置开始到该字符串的结尾。

strstr

查找字符串的首次出现,返回字符串从第一次出现的位置开始到该字符串的结尾或开始。

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

参数说明

haystack在该字符串中进行查找。 needle如果 needle 不是一个字符串,那么它将被转换为整型并被视为字符的顺序值来使用。 before_needle若为 TRUE,strstr() 将返回 needle 在 haystack 中的位置之前的部分。

返回值

成功:返回字符串 needle 之前或之后的一部分 失败:如果没找到 needle,将返回 FALSE。

注意

  1. 该函数区分大小写
  2. 如果你仅仅想确定 needle 是否存在于 haystack 中,请使用速度更快、耗费内存更少的 strpos()函数

示例

<?php  /*【 needle 为单个字符 】 */$email  = 'name@example.com';$domain = strstr($email, '@');echo $domain; // 打印 @example.com$user = strstr($email, '@', true); // 从 PHP 5.3.0 起echo $user; // 打印 name  ?>
<?php  /*【 needle 为数字 】 */$email  = 'name@example.com'; //字母a的 ASCII码为 97$behind = strstr($email, 97);echo $behind; // 打印 ame@example.com$front = strstr($email, 97, true); // 从 PHP 5.3.0 起echo $front; // 打印 n  ?>
<?php  /*【 needle 为字符串 】 */$email = 'name@example.com';$behind  = strstr($email, 'ex');echo $behind; // 打印 example.com$front = strstr($email, 'ex', true); // 从 PHP 5.3.0 起echo $front; // 打印 name@  */?>
<?php  /*【 needle 为字符串 】 */$email = 'name@example.com';$behind  = strstr($email, 'ab');echo $behind; // 返回 false$front = strstr($email, 'ab', true); // 从 PHP 5.3.0 起echo $front; // 返回 false  */?>

stristr

strstr() 函数的忽略大小写版本

mixed stristr ( string $haystack , mixed $needle [, bool $before_needle = false ] )  

该函数与 strstr() 唯一的区别就是不区分大小写。其他可参考strstr()

<?php  $email  = 'name@example.com';$behind = stristr($email, 'A');echo $behind; // 打印 ame@example.com$front = stristr($email, 'A', true); // 从 PHP 5.3.0 起echo $front; // 打印 n  ?>

strchr

strstr() 函数的别名

mixed strchr ( string $haystack , mixed $needle [, bool $before_needle = false ] )  

该函数等同 strstr() 。其他可参考strstr()

$email  = 'name@example.com';$behind = strchr($email, 'a');echo $behind; // 打印 ame@example.com$front = strchr($email, 'a', true); // 从 PHP 5.3.0 起echo $front; // 打印 n  ?>

strrchr

查找字符串的最后一次出现,返回字符串从最后一次出现的位置开始到该字符串的结尾。

mixed strrchr ( string $haystack , mixed $needle )  

参数说明

haystack在该字符串中进行查找。 needle如果 needle 包含了不止一个字符,那么仅使用第一个字符。该行为不同于 strstr()。 如果 needle 不是一个字符串,那么将被转化为整型并被视为字符顺序值。

返回值

成功:返回字符串 needle 之后的一部分 失败:如果没找到 needle,将返回 FALSE。

示例

<?php  /*【 needle 为字符 】 */$email  = 'name@example.com';$behind = strrchr($email, 'a');echo $behind; // 打印 ample.com  ?>
/*【 needle 为字符串 】 */$email  = 'name@example.com';$behind = strrchr($email, 'am');echo $behind; // 打印 ample.com  ?>
<?php  /*【 needle 为数字 】 */$email  = 'name@example.com';$behind = strrchr($email, 97);echo $behind; // 打印 ample.com  ?>

OneAPM for PHP 能够深入到所有 PHP 应用内部完成应用性能管理 能够深入到所有 PHP 应用内部完成应用性能管理和监控,包括代码级别性能问题的可见性、性能瓶颈的快速识别与追溯、真实用户体验监控、服务器监控和端到端的应用性能管理。想阅读更多技术文章,请访问 OneAPM官方技术博客。

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
Working with Flash Session Data in LaravelWorking with Flash Session Data in LaravelMar 12, 2025 pm 05:08 PM

Laravel simplifies handling temporary session data using its intuitive flash methods. This is perfect for displaying brief messages, alerts, or notifications within your application. Data persists only for the subsequent request by default: $request-

cURL in PHP: How to Use the PHP cURL Extension in REST APIscURL in PHP: How to Use the PHP cURL Extension in REST APIsMar 14, 2025 am 11:42 AM

The PHP Client URL (cURL) extension is a powerful tool for developers, enabling seamless interaction with remote servers and REST APIs. By leveraging libcurl, a well-respected multi-protocol file transfer library, PHP cURL facilitates efficient execution of various network protocols, including HTTP, HTTPS, and FTP. This extension offers granular control over HTTP requests, supports multiple concurrent operations, and provides built-in security features.

PHP Logging: Best Practices for PHP Log AnalysisPHP Logging: Best Practices for PHP Log AnalysisMar 10, 2025 pm 02:32 PM

PHP logging is essential for monitoring and debugging web applications, as well as capturing critical events, errors, and runtime behavior. It provides valuable insights into system performance, helps identify issues, and supports faster troubleshoot

Simplified HTTP Response Mocking in Laravel TestsSimplified HTTP Response Mocking in Laravel TestsMar 12, 2025 pm 05:09 PM

Laravel provides concise HTTP response simulation syntax, simplifying HTTP interaction testing. This approach significantly reduces code redundancy while making your test simulation more intuitive. The basic implementation provides a variety of response type shortcuts: use Illuminate\Support\Facades\Http; Http::fake([ 'google.com' => 'Hello World', 'github.com' => ['foo' => 'bar'], 'forge.laravel.com' =>

12 Best PHP Chat Scripts on CodeCanyon12 Best PHP Chat Scripts on CodeCanyonMar 13, 2025 pm 12:08 PM

Do you want to provide real-time, instant solutions to your customers' most pressing problems? Live chat lets you have real-time conversations with customers and resolve their problems instantly. It allows you to provide faster service to your custom

Explain the concept of late static binding in PHP.Explain the concept of late static binding in PHP.Mar 21, 2025 pm 01:33 PM

Article discusses late static binding (LSB) in PHP, introduced in PHP 5.3, allowing runtime resolution of static method calls for more flexible inheritance.Main issue: LSB vs. traditional polymorphism; LSB's practical applications and potential perfo

Customizing/Extending Frameworks: How to add custom functionality.Customizing/Extending Frameworks: How to add custom functionality.Mar 28, 2025 pm 05:12 PM

The article discusses adding custom functionality to frameworks, focusing on understanding architecture, identifying extension points, and best practices for integration and debugging.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft