Search method: 1. Use strpos(), the syntax "strpos("String value","Search substring") 1"; 2. Use stripos(), the syntax "strpos("String value ","Find substring ") 1". Because strings start counting from 0, the positions obtained by the two functions need to be incremented by 1.
The operating environment of this tutorial: windows7 system, PHP version 8.1, DELL G3 computer
In PHP, if you want to find a string, There are two functions to determine the number:
strpos()
stripos()
Both functions can find the first occurrence of a string, and the syntax is similar:
strpos(string,find,start) stripos(string,find,start)
Parameters | Description |
---|---|
string | Required. Specifies the string to be searched for. |
find | Required. Specifies the characters to search for. |
start | Optional. Specifies the location from which to start the search. |
#Return value: Returns the position of the first occurrence of a string in another string, or FALSE if the string is not found.
But the strpos() function is case-sensitive, while the stripos() function is not case-sensitive.
Because strings start counting from 0, the positions obtained by the strpos() and stripos() functions need to be incremented by 1.
Example 1: Use the strpos() function to find the number of the string
<?php header('content-type:text/html;charset=utf-8'); $mystring = 'ABCabc'; $findme1 = 'c'; $pos1 = strpos($mystring, $findme1)+1; echo $findme1." 在第 ".$pos1." 位<br>"; $findme2 = 'C'; $pos2 = strpos($mystring, $findme2)+1; echo $findme2." 在第 ".$pos2." 位<br>"; $findme3 = 'Ca'; $pos3 = strpos($mystring, $findme3)+1; echo $findme3." 在第 ".$pos3." 位<br>"; ?>
Example 2: Use The stripos() function finds the number of a string
<?php header('content-type:text/html;charset=utf-8'); $mystring = 'ABCabc'; $findme1 = 'c'; $pos1 = stripos($mystring, $findme1)+1; echo $findme1." 在第 ".$pos1." 位<br>"; $findme2 = 'C'; $pos2 = stripos($mystring, $findme2)+1; echo $findme2." 在第 ".$pos2." 位<br>"; $findme3 = 'aB'; $pos3 = stripos($mystring, $findme3)+1; echo $findme3." 在第 ".$pos3." 位<br>"; ?>
Recommended study: "PHP Video Tutorial"
The above is the detailed content of How to find the number of a string in php. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 English version
Recommended: Win version, supports code prompts!

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),
