Table of Contents
strstr
参数说明
返回值
注意
示例
stristr
strchr
strrchr
OneAPM for PHP 能够深入到所有 PHP 应用内部完成应用性能管理 能够深入到所有 PHP 应用内部完成应用性能管理和监控,包括代码级别性能问题的可见性、性能瓶颈的快速识别与追溯、真实用户体验监控、服务器监控和端到端的应用性能管理。想阅读更多技术文章,请访问 OneAPM官方技术博客。
Home Backend Development PHP Tutorial PHP字符串函数之 strstr stristr strchr strrchr

PHP字符串函数之 strstr stristr strchr strrchr

Jun 20, 2016 pm 12:33 PM

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

strstr

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

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

参数说明

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  ?>
Copy after login
<?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  ?>
Copy after login
<?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@  */?>
Copy after login
<?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  */?>
Copy after login

stristr

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

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

该函数与 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  ?>
Copy after login

strchr

strstr() 函数的别名

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

该函数等同 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  ?>
Copy after login

strrchr

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

mixed strrchr ( string $haystack , mixed $needle )  
Copy after login

参数说明

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

返回值

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

示例

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

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

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

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)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Two Point Museum: All Exhibits And Where To Find Them
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Working with Flash Session Data in Laravel Working with Flash Session Data in Laravel Mar 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 APIs cURL in PHP: How to Use the PHP cURL Extension in REST APIs Mar 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.

Simplified HTTP Response Mocking in Laravel Tests Simplified HTTP Response Mocking in Laravel Tests Mar 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 CodeCanyon 12 Best PHP Chat Scripts on CodeCanyon Mar 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

PHP Logging: Best Practices for PHP Log Analysis PHP Logging: Best Practices for PHP Log Analysis Mar 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

Discover File Downloads in Laravel with Storage::download Discover File Downloads in Laravel with Storage::download Mar 06, 2025 am 02:22 AM

The Storage::download method of the Laravel framework provides a concise API for safely handling file downloads while managing abstractions of file storage. Here is an example of using Storage::download() in the example controller:

HTTP Method Verification in Laravel HTTP Method Verification in Laravel Mar 05, 2025 pm 04:14 PM

Laravel simplifies HTTP verb handling in incoming requests, streamlining diverse operation management within your applications. The method() and isMethod() methods efficiently identify and validate request types. This feature is crucial for building

See all articles