Home headlines PHP string function (2): comparison operation

PHP string function (2): comparison operation

Jun 28, 2018 am 10:39 AM

* 1.strcmp($str1, $str2): Binary safe string comparison

* 2.strncmp($str1, $str2, $length): Compare whether the length specified at the beginning is the same

* 3.strcasecmp($str1, $str2): Binary safe string comparison, case-insensitive

* 4.strncasecmp($str1, $str2): Binary safe string comparison , case-insensitive

* 5.strspn($str,$mark,$start,$length): Get the length of the starting substring matching the mask

* 6. strcspn($str,$mark,$start,$length): Get the length of the starting substring of the unmatched mask

$str1 = 'php中文网';
$str2 = 'PHP中文网';
Copy after login

//1.strcmp($str1, $str2):String For comparison, return 0 if equal, return >0 if greater, otherwise return <0

echo strcmp($str1, $str2) == 0 ? &#39;相等&#39; : &#39;不相等&#39;, &#39;<br>&#39;;
Copy after login

//2.strncmp($str, $str2, $n): Compare whether the lengths specified at the beginning are equal

echo strncmp($str1, $str2, 3) == 0 ? &#39;相等&#39; : &#39;不相等&#39;, &#39;<br>&#39;;
Copy after login

//3.strcasecmp($str1, $str2): case-insensitive string comparison, returns 0 if equal, >0 if greater, otherwise returns <0

echo strcasecmp($str1, $str2) == 0 ? &#39;相等&#39; : &#39;不相等&#39;, &#39;<br>&#39;;
Copy after login

//4 .strncasecmp($str1, $str2): Case-insensitive comparison of whether the length specified at the beginning is equal

echo strncasecmp($str1, $str2,3) == 0 ? &#39;相等&#39; : &#39;不相等&#39;, &#39;<br>&#39;;
Copy after login

//5.strspn($str, $mark, $start, $length):

//Calculate the length of the first substring in which all characters in the string exist in the specified character set

//$str1: The string to be compared, $mark: Similar to a set, return Number of matches

echo strspn(&#39;15705519989&#39;, &#39;1234567890&#39;),&#39;<br>&#39;;  //返回11
Copy after login

//You can specify the position and length to start comparison

echo strspn(&#39;15705519989&#39;, &#39;1234567890&#39;, 4, 4),&#39;<br>&#39;;//返回4
Copy after login

//Only compare the first substring in $str, ignore all the following ones, and return 11

echo strspn(&#39;15705519989 18955123344 111abc&#39;, &#39;1234567890&#39;),&#39;<br>&#39;;
Copy after login

//Return 3, because only the first three in the first string are data belonging to the number set

echo strspn(&#39;157php 18955123344 111abc&#39;, &#39;1234567890&#39;),&#39;<br>&#39;;
Copy after login

//For example, the mobile phone number must be a pure numeric string, requiring the user What must be entered is a purely numeric string

$phone = &#39;13899886767&#39;;
Copy after login

// $phone = '1389988php6767';

$mark = &#39;0123456789&#39;;
Copy after login

//Analysis, if it matches correctly, strspn() must return 11, because the phone The number is 11, which is exactly the same as strlen($phone)

echo strlen($phone)==strspn($phone, $mark) ? &#39;全数字&#39; : &#39;必须全为数字&#39;;
Copy after login

//Equivalent to: strspn(substr($subject, $start, $length), $mask)

// 6. The functions of strcspn() and strspn() are exactly opposite. You can verify it by giving examples

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)

How to convert characters to ascii code in php How to convert characters to ascii code in php Mar 03, 2023 pm 06:55 PM

In PHP, you can use the ord() function to convert characters into ascii code. This function can return the ASCII value of a single character or the first character in a string. The returned ASCII value will be displayed in integer form; the conversion syntax "ord (string)", the parameter "string" cannot be omitted, it is the string (or single character) from which the ASCII value is to be obtained.

How to replace a certain character to be empty in a php string How to replace a certain character to be empty in a php string Mar 06, 2023 pm 06:39 PM

There are two ways to replace a certain character with a null character in a PHP string: 1. Use the str_replace() function to replace the specified character with a null character. You only need to set the first parameter to the specified character and the second parameter to a null character. Syntax "str_replace("specified character","", $str)"; 2. Use the preg_replace() function with regular expressions to match the specified character and replace it with the null character, syntax "preg_replace('/specified character/', "",$str)".

How to remove all uppercase letters from string in php How to remove all uppercase letters from string in php Sep 26, 2022 pm 07:59 PM

Two removal methods: 1. Use preg_replace() to execute a regular expression to search for all uppercase letters and replace them with null characters. The syntax is "preg_replace('/[A-Z]/','',$str)". 2. Use preg_filter() to execute a regular expression to search for all uppercase letters and replace them with empty characters. The syntax is "preg_filter('/[A-Z]/','',$str)".

How to remove left and right characters from string in php How to remove left and right characters from string in php Mar 27, 2023 pm 03:29 PM

PHP is a typed programming language that is often used to develop web applications. During web development, you may need to perform various operations on strings, such as removing specific characters from a string, retaining numbers or letters in a string, etc. In this article, we will focus on how to remove specific characters on the left or right side of a string in PHP.

How to remove double quotes from string in php How to remove double quotes from string in php Mar 28, 2023 pm 04:54 PM

PHP is a very popular programming language and one of the preferred tools for building dynamic websites. In PHP development, we often need to operate strings, and one common requirement is to remove double quotes from strings. In this article, we will introduce some methods to remove double quotes from PHP strings.

How to extract only Chinese characters from php string How to extract only Chinese characters from php string Sep 22, 2022 pm 07:44 PM

Two methods: 1. Use preg_match_all() with regular filter strings, the syntax is "preg_match_all("/[\x{4e00}-\x{9fff}]+/u","$str",$arr);" ; 2. Use preg_replace() with regular search for non-Chinese letters in the string and replace them with empty characters. The syntax is "preg_replace("/[^\x{4E00}-\x{9FFF}]+/u" ,'',$str)".

Is it possible to add characters to a string in php? Is it possible to add characters to a string in php? Aug 19, 2022 pm 07:51 PM

PHP can add characters to strings. Two implementation methods: 1. Use the string connector "." to splice the specified character to the beginning or end of the string. The syntax is "specified character. string" or "string. specified character"; 2. use substr_replace The () function can insert the specified character at the specified position of the string. The syntax is "substr_replace(string, specified character, specified position, 0)". The value at the specified position can be 0, negative or positive.

What are the string delimiters in php? What are the string delimiters in php? Sep 23, 2022 pm 05:48 PM

There are two types of PHP string delimiters: 1. Heredoc delimiter. After the "<<<" operator, an identifier must be provided, followed by a newline, followed by the string itself, and finally the previously defined identifier must be used. as an end sign. 2. Nowdoc delimiter. After the "<<<" operator, an identifier enclosed in single quotes must be provided, followed by a newline, followed by the string itself, and finally the previously defined identifier must be used as the end mark.