search
HomeBackend DevelopmentPHP Tutorialphp strstr() strrchr() strpos() strrpos() function_PHP tutorial

php strstr() strrchr() strpos() strrpos() function_PHP tutorial

Jul 13, 2016 pm 04:59 PM
phpstrposstrrposhostfunctionjudgmentcharacteroperateyes

These four functions are character operation functions, which mainly determine the number of times a character appears. Friends in need can refer to it.

The code is as follows
 代码如下 复制代码

//strstr:从左向右查找  返回值:字符串
//strrchr:从右向左查找 返回值:字符串
//strpos:从左向右查找  返回值:整型,假如查找的字符串不存在,则返回空
//strrpos:从右向左查找 返回值:整型
$str="天高任鸟飞,海阔凭鱼跃";//strstr:从左向右查找  strrchr:从右向左查找
echo "原始字符串:".$str."
";
echo "用strstr函数搜索“,”的返回结果:".strstr($str,",")."
";
echo "用strstr函数搜索“鸟飞”的返回结果:".strstr($str,"鸟飞")."
";

$str1="I have a great dream.";
echo "原始字符串:".$str1."
";
echo "用strrchr函数搜索“e”的返回结果:".strrchr($str1,"e")."
";
echo "用strrchr函数搜索“ea”的返回结果:".strrchr($str1,"ve")."
";

$str2="I am an abstract about abroad.";
echo "原始字符串为:".$str2."
";
echo "ab在字符串中的第一次出现位置为:".strpos($str2,"ab")."
";
echo "ab在字符串中的第一次出现位置为:".strpos($str2,"am")."
";
echo "abcd在字符串中第一次出现的位置为:".strpos($str2,"aman")."
";

$str3="I am is wang hui.";
echo "原始字符串为:".$str3."
";
echo "I在字符串中的最后一次出现位置为:".strrpos($str3,"I")."
";
echo "an在字符串中的最后一次出现位置为:".strrpos($str3,"an")."
";
echo "i在字符串中最后一次出现的位置为:".strrpos($str3,"i");

?>

Copy code
//strstr: Search from left to right Return value: string
//strrchr: Search from right to left Return value: string
//strpos: Search from left to right. Return value: integer. If the searched string does not exist, it will return empty
//strrpos: Search from right to left Return value: integer
$str="The sky is high enough for birds to fly, and the sea is wide enough for fish to jump";//strstr: Search from left to right strrchr: Search from right to left
echo "Original string: ".$str."
";
echo "The return result of searching for "," using strstr function: ".strstr($str,",")."
";
echo "The return result of searching for "bird flying" using strstr function:".strstr($str,"鸟飞")."
";

$str1="I have a great dream.";
echo "Original string:".$str1."
";
echo "The return result of searching for "e" using strrchr function: ".strrchr($str1,"e")."
";
echo "The return result of searching for "ea" using strrchr function: ".strrchr($str1,"ve")."
";

$str3="I am is wang hui.";
echo "The original string is: ".$str3."
";
echo "The last occurrence of I in the string is: ".strrpos($str3,"I")."
";
echo "The last occurrence of an in the string is: ".strrpos($str3,"an")."
";
echo "The last occurrence of i in the string is: ".strrpos($str3,"i");
?>
http://www.bkjia.com/PHPjc/631322.html
www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/631322.htmlTechArticleThese four functions are character operation functions, mainly to determine the number of times a character appears. Friends in need can refer to it. . The code is as follows Copy code ?php //strstr: Search from left to right Return...
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 Email: Step-by-Step Sending GuidePHP Email: Step-by-Step Sending GuideMay 09, 2025 am 12:14 AM

PHPisusedforsendingemailsduetoitsintegrationwithservermailservicesandexternalSMTPproviders,automatingnotificationsandmarketingcampaigns.1)SetupyourPHPenvironmentwithawebserverandPHP,ensuringthemailfunctionisenabled.2)UseabasicscriptwithPHP'smailfunct

How to Send Email via PHP: Examples & CodeHow to Send Email via PHP: Examples & CodeMay 09, 2025 am 12:13 AM

The best way to send emails is to use the PHPMailer library. 1) Using the mail() function is simple but unreliable, which may cause emails to enter spam or cannot be delivered. 2) PHPMailer provides better control and reliability, and supports HTML mail, attachments and SMTP authentication. 3) Make sure SMTP settings are configured correctly and encryption (such as STARTTLS or SSL/TLS) is used to enhance security. 4) For large amounts of emails, consider using a mail queue system to optimize performance.

Advanced PHP Email: Custom Headers & FeaturesAdvanced PHP Email: Custom Headers & FeaturesMay 09, 2025 am 12:13 AM

CustomheadersandadvancedfeaturesinPHPemailenhancefunctionalityandreliability.1)Customheadersaddmetadatafortrackingandcategorization.2)HTMLemailsallowformattingandinteractivity.3)AttachmentscanbesentusinglibrarieslikePHPMailer.4)SMTPauthenticationimpr

Guide to Sending Emails with PHP & SMTPGuide to Sending Emails with PHP & SMTPMay 09, 2025 am 12:06 AM

Sending mail using PHP and SMTP can be achieved through the PHPMailer library. 1) Install and configure PHPMailer, 2) Set SMTP server details, 3) Define the email content, 4) Send emails and handle errors. Use this method to ensure the reliability and security of emails.

What is the best way to send an email using PHP?What is the best way to send an email using PHP?May 08, 2025 am 12:21 AM

ThebestapproachforsendingemailsinPHPisusingthePHPMailerlibraryduetoitsreliability,featurerichness,andeaseofuse.PHPMailersupportsSMTP,providesdetailederrorhandling,allowssendingHTMLandplaintextemails,supportsattachments,andenhancessecurity.Foroptimalu

Best Practices for Dependency Injection in PHPBest Practices for Dependency Injection in PHPMay 08, 2025 am 12:21 AM

The reason for using Dependency Injection (DI) is that it promotes loose coupling, testability, and maintainability of the code. 1) Use constructor to inject dependencies, 2) Avoid using service locators, 3) Use dependency injection containers to manage dependencies, 4) Improve testability through injecting dependencies, 5) Avoid over-injection dependencies, 6) Consider the impact of DI on performance.

PHP performance tuning tips and tricksPHP performance tuning tips and tricksMay 08, 2025 am 12:20 AM

PHPperformancetuningiscrucialbecauseitenhancesspeedandefficiency,whicharevitalforwebapplications.1)CachingwithAPCureducesdatabaseloadandimprovesresponsetimes.2)Optimizingdatabasequeriesbyselectingnecessarycolumnsandusingindexingspeedsupdataretrieval.

PHP Email Security: Best Practices for Sending EmailsPHP Email Security: Best Practices for Sending EmailsMay 08, 2025 am 12:16 AM

ThebestpracticesforsendingemailssecurelyinPHPinclude:1)UsingsecureconfigurationswithSMTPandSTARTTLSencryption,2)Validatingandsanitizinginputstopreventinjectionattacks,3)EncryptingsensitivedatawithinemailsusingOpenSSL,4)Properlyhandlingemailheaderstoa

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 Tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

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.