search
HomeBackend DevelopmentPHP TutorialPHP string interception tutorial

PHP string interception tutorial

Jul 29, 2016 am 09:04 AM
phpstring

获Php string Get SUBSTR and StRSTR Function

PHP string interception tutorial

Recommended tutorial: Php online tutorial


Php string to get

to obtain specified characters from the string string. Relevant functions are as follows:

 ●substr(): Get a part of it from a string

 ●strstr(): Find the position where the string first appears in another string, and return the position from that position to All characters at the end of the string

 ●subchr(): Same as strstr()

 ●strrchr(): Find the last occurrence of a string in another string and return all characters from that position to the end of the string Character

substr()

Syntax:

strrchr(): The function is used to get a part of it from a string and return a string.

string substr ( string string, int start [, int length] )
The parameter description is as follows:

Parameter DescriptionstringThe string to be processedstartStart position of the string, the starting position is 0, if it is negative, start from Starting from the specified position at the end of the stringlengthOptional, the length returned by the string, the default is until the end of the string, if it is negative, it is returned from the end of the stringExample:
<?php
echo substr(&#39;abcdef&#39;, 1);      //输出 bcdef
echo substr(&#39;abcdef&#39;, 1, 2);   //输出 bc
echo substr(&#39;abcdef&#39;, -3, 2);  //输出 de
echo substr(&#39;abcdef&#39;, 1, -2);  //输出 bcd
?>

Tips: If start is a negative number and length is less than or equal to start, then length is 0.

strstr()

Find the first occurrence of a string in another string and return all characters from that position to the end of the string, or FALSE if not found. Syntax:

string strstr ( string string, string needle )

The parameter description is as follows:

Parameter DescriptionstringThe string to be processed needleTo be The string to search for, if it is a number , then search for characters matching the numeric ASCII value

例子:

<?php
$email = &#39;user@5idev.com&#39;;
$domain = strstr($email, &#39;@&#39;);
echo $domain;			// 输出 @5idev.com
?>

提示:该函数对大小写敏感。如需进行大小写不敏感的查找,请使用 stristr() 。

strchr()

同 strstr() 。

strrchr()

查找字符串在另一个字符串中最后一次出现的位置,并返回从该位置到字符串结尾的所有字符,如果没找到则返回 FALSE。

语法:

string strrchr ( string string, string needle )

该函数行为同 strstr() 函数,参数意义可参见上面 strstr() 函数参数说明。

例子:

<?php
$str="AAA|BBB|CCC";
echo strrchr($str, "|");
?>

运行例子,输出:

|CCC

结合 substr() 函数便可以实现 截取某个最后出现的字符后面的所有内容 这一功能:

<?php
$str="AAA|BBB|CCC";
echo substr(strrchr($str, "|"), 1);
?>
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
PHP Performance Tuning for High Traffic WebsitesPHP Performance Tuning for High Traffic WebsitesMay 14, 2025 am 12:13 AM

ThesecrettokeepingaPHP-poweredwebsiterunningsmoothlyunderheavyloadinvolvesseveralkeystrategies:1)ImplementopcodecachingwithOPcachetoreducescriptexecutiontime,2)UsedatabasequerycachingwithRedistolessendatabaseload,3)LeverageCDNslikeCloudflareforservin

Dependency Injection in PHP: Code Examples for BeginnersDependency Injection in PHP: Code Examples for BeginnersMay 14, 2025 am 12:08 AM

You should care about DependencyInjection(DI) because it makes your code clearer and easier to maintain. 1) DI makes it more modular by decoupling classes, 2) improves the convenience of testing and code flexibility, 3) Use DI containers to manage complex dependencies, but pay attention to performance impact and circular dependencies, 4) The best practice is to rely on abstract interfaces to achieve loose coupling.

PHP Performance: is it possible to optimize the application?PHP Performance: is it possible to optimize the application?May 14, 2025 am 12:04 AM

Yes,optimizingaPHPapplicationispossibleandessential.1)ImplementcachingusingAPCutoreducedatabaseload.2)Optimizedatabaseswithindexing,efficientqueries,andconnectionpooling.3)Enhancecodewithbuilt-infunctions,avoidingglobalvariables,andusingopcodecaching

PHP Performance Optimization: The Ultimate GuidePHP Performance Optimization: The Ultimate GuideMay 14, 2025 am 12:02 AM

ThekeystrategiestosignificantlyboostPHPapplicationperformanceare:1)UseopcodecachinglikeOPcachetoreduceexecutiontime,2)Optimizedatabaseinteractionswithpreparedstatementsandproperindexing,3)ConfigurewebserverslikeNginxwithPHP-FPMforbetterperformance,4)

PHP Dependency Injection Container: A Quick StartPHP Dependency Injection Container: A Quick StartMay 13, 2025 am 12:11 AM

APHPDependencyInjectionContainerisatoolthatmanagesclassdependencies,enhancingcodemodularity,testability,andmaintainability.Itactsasacentralhubforcreatingandinjectingdependencies,thusreducingtightcouplingandeasingunittesting.

Dependency Injection vs. Service Locator in PHPDependency Injection vs. Service Locator in PHPMay 13, 2025 am 12:10 AM

Select DependencyInjection (DI) for large applications, ServiceLocator is suitable for small projects or prototypes. 1) DI improves the testability and modularity of the code through constructor injection. 2) ServiceLocator obtains services through center registration, which is convenient but may lead to an increase in code coupling.

PHP performance optimization strategies.PHP performance optimization strategies.May 13, 2025 am 12:06 AM

PHPapplicationscanbeoptimizedforspeedandefficiencyby:1)enablingopcacheinphp.ini,2)usingpreparedstatementswithPDOfordatabasequeries,3)replacingloopswitharray_filterandarray_mapfordataprocessing,4)configuringNginxasareverseproxy,5)implementingcachingwi

PHP Email Validation: Ensuring Emails Are Sent CorrectlyPHP Email Validation: Ensuring Emails Are Sent CorrectlyMay 13, 2025 am 12:06 AM

PHPemailvalidationinvolvesthreesteps:1)Formatvalidationusingregularexpressionstochecktheemailformat;2)DNSvalidationtoensurethedomainhasavalidMXrecord;3)SMTPvalidation,themostthoroughmethod,whichchecksifthemailboxexistsbyconnectingtotheSMTPserver.Impl

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools