search
HomeBackend DevelopmentPHP ProblemHow to detect whether a string contains specified characters in php

Two detection methods: 1. Use strpos() to detect the position where the specified character first appears in the string. The syntax is "strpos(string, character, starting position)". If it returns the position representing the position A number means it is included, and if it returns FALSE it means it is not included. 2. Use strrpos() to detect the position of the last occurrence of the specified character in the string. The syntax is "strrpos(string, character, starting position)". If a number representing the position is returned, it means it is included. If it returns FALSE, it means it is not included. .

How to detect whether a string contains specified characters in php

The operating environment of this tutorial: windows7 system, PHP version 8.1, DELL G3 computer

php detection string Methods to determine whether a string contains specified characters

Method 1: Use the strpos() function to detect

The strpos() function can find strings in another string The position of the first occurrence in a string (case sensitive).

If the specified character exists, return the position of the first occurrence; if not found, return FALSE.

Note: The string position starts from 0, not from 1.

Example:

<?php
header(&#39;content-type:text/html;charset=utf-8&#39;);   
$findme = &#39;C&#39;;
$mystring1 = &#39;xyz&#39;;
$mystring2 = &#39;ABC&#39;;
$pos1 = strpos($mystring1, $findme);
$pos2 = strpos($mystring2, $findme);
var_dump($pos1);
var_dump($pos2);
if($pos1){
	echo $mystring1." 中指定字符 C<br>";
}else{
	echo $mystring1." 中不包含指定字符 C<br>";
}
if($pos2){
	echo $mystring2." 指定字符 C<br>";
}else{
	echo $mystring2." 不包含指定字符 C<br>";
}
?>

How to detect whether a string contains specified characters in php

Method 2: Use strrpos() function to detect

strrpos() function can Finds the last occurrence of a string within another string (case sensitive).

If the specified character exists, return the position of the last occurrence; if not found, return FALSE.

Example:

<?php
header(&#39;content-type:text/html;charset=utf-8&#39;);   
$findme = &#39;c&#39;;
$mystring1 = &#39;xyzc&#39;;
$mystring2 = &#39;ABC&#39;;
$pos1 = strrpos($mystring1, $findme);
$pos2 = strrpos($mystring2, $findme);
var_dump($pos1);
var_dump($pos2);
if($pos1){
	echo $mystring1." 中指定字符 c<br>";
}else{
	echo $mystring1." 中不包含指定字符 c<br>";
}
if($pos2){
	echo $mystring2." 指定字符 c<br>";
}else{
	echo $mystring2." 不包含指定字符 c<br>";
}
?>

How to detect whether a string contains specified characters in php

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of How to detect whether a string contains specified characters in php. For more information, please follow other related articles on the PHP Chinese website!

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

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

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment