Home php教程 php手册 php中查找字符吕函数之strstr,strpos,substr,strrchr介绍

php中查找字符吕函数之strstr,strpos,substr,strrchr介绍

Jun 13, 2016 am 10:15 AM
php strpos substr introduce function exist character us Find

我们在php中查找字符是否存在字符串中一般会用到函数strstr,strpos,substr,strrchr,stripos这几个常用的字符查找函数,有需了解的同学可看看。

一、strstr — 查找字符串的首次出现

string strstr ( string $haystack , mixed $needle [, bool $before_needle = false ] )
注1:$haystack是当事字符串,$needle是被查找的字符串。该函数区分大小写。
注2:返回值是从needle开始到最后。
注3:关于$needle,如果不是字符串,被当作整形来作为字符的序号来使用。
注4:before_needle若为true,则返回前东西。

 代码如下 复制代码
$email = 'yuxiaoxiao@example.com';
$domain = strstr($email, '@');
echo $domain; // 打印 @example.com
$user = strstr($email, '@', true); // 从 PHP 5.3.0 起
echo $user; // 打印 yuxiaoxiao
?>


二、stristr strstr不区分大小写的版本
三、strpos -查找字符串首次出现的位置

int strpos ( string $haystack , mixed $needle [, int $offset = 0 ] )
注1:可选的 offset 参数可以用来指定从 haystack 中的哪一个字符开始查找。返回的数字位置是相对于 haystack 的起始位置而言的。
四、substr -返回字符串的子串

string substr ( string $string , int $start [, int $length ] )
$rest = substr("abcdef", -1); // 返回 "f"

注1:如果 start 是非负数,返回的字符串将从 string 的 start 位置开始,从 0 开始计算。例如,在字符串 “abcdef” 中,在位置 0 的字符是 “a”,位置 2 的字符串是 “c” 等等。

注2:如果 start 是负数,返回的字符串将从 string 结尾处向前数第 start 个字符开始。

注3:如果 string 的长度小于或等于 start,将返回 FALSE。

length

注4:如果提供了正数的 length,返回的字符串将从 start 处开始最多包括 length 个字符(取决于 string 的长度)。

注5:如果提供了负数的 length,那么 string 末尾处的许多字符将会被漏掉(若 start 是负数则从字符串尾部算起)。如果 start 不在这段文本中,那么将返回一个空字符串。

注6:如果提供了值为 0,FALSE 或 NULL 的 length,那么将返回一个空字符串。

注7:如果没有提供 length,返回的子字符串将从 start 位置开始直到字符串结尾。

 代码如下 复制代码
$rest = substr("abcdef", 0, -1); // 返回 "abcde"
$rest = substr("abcdef", 2, -1); // 返回 "cde"
$rest = substr("abcdef", 4, -4); // 返回 ""
$rest = substr("abcdef", -3, -1); // 返回 "de"
?>

 

五、strrchr -查找指定字符在字符串中的最后一次出现

string strrchr ( string $haystack , mixed $needle )

该函数返回 haystack 字符串中的一部分,这部分以 needle 的最后出现位置开始,直到 haystack 末尾。

六、strripos -计算指定字符串在目标字符串中最后一次出现的位置(不区分大小写)
七、stripos -查找字符串首次出现的位置(不区分大小定)
八、strrpos -计算指定字符串在目标字符串中最后一次出现的位置

这些函数只会返回你要查找的字符是否存了字符串中并返回位置或返回0 or 1,如果要从指定位置取我们可以利用substr等等这些函数来组合操作哦。

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)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks 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)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

To work on file upload we are going to use the form helper. Here, is an example for file upload.

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

Validator can be created by adding the following two lines in the controller.

CakePHP Logging CakePHP Logging Sep 10, 2024 pm 05:26 PM

Logging in CakePHP is a very easy task. You just have to use one function. You can log errors, exceptions, user activities, action taken by users, for any background process like cronjob. Logging data in CakePHP is easy. The log() function is provide

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

CakePHP Quick Guide CakePHP Quick Guide Sep 10, 2024 pm 05:27 PM

CakePHP is an open source MVC framework. It makes developing, deploying and maintaining applications much easier. CakePHP has a number of libraries to reduce the overload of most common tasks.

See all articles