Home >headlines >PHP string function (2): comparison operation

PHP string function (2): comparison operation

无忌哥哥
无忌哥哥Original
2018-06-28 10:39:203224browse

* 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中文网';

//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;;

//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;;

//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;;

//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;;

//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

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

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

//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;;

//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;;

//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;;

// $phone = '1389988php6767';

$mark = &#39;0123456789&#39;;

//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;;

//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:
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