者相等,返回 0
<?php
$arr1 = $arr2 = array("img12.png", "img10.png", "img2.png", "img1.png");
usort($arr1, "strcmp");
print_r($arr1);//Array ( [0] => img1.png [1] => img10.png [2] => img12.png [3] => img2.png )
usort($arr2, "strnatcmp");//Array ( [0] => img1.png [1] => img2.png [2] => img10.png [3] => img12.png )
print_r($arr2);
?>
位置
strstr()
strstr()查找字符串的首次出现,返回haystack字符串从needle第一次出现的位置开始到haystack结尾的字符串
string strstr ( string $haystack , mixed $needle [, bool $before_needle = false ] )
before_needle若为TRUE,strstr()将返回needle在haystack中的位置之前的部分
<?php
$email = 'name@example.com';
$domain = strstr($email, '@');
echo $domain; // @example.com
$user = strstr($email, '@', true);
echo $user; //name
?>
strpos()
strpos()查找字符串首次出现的位置
mixed strpos ( string $haystack , mixed $needle [, int $offset = 0 ] )
<?php
// 忽视位置偏移量之前的字符进行查找
$newstring = 'abcdef abcdef';
$pos = strpos($newstring, 'a', 1); // $pos = 7, 不是 0
?>
strrpos()
strrpos()计算指定字符串在目标字符串中最后一次出现的位置
int strrpos ( string $haystack , string $needle [, int $offset = 0 ] )
<?php
$foo = "0123456789a123456789b123456789c";
var_dump(strrpos($foo, '7', -5)); // 从尾部第 5 个位置开始查找
// 结果: int(17)
var_dump(strrpos($foo, '7', 20)); // 从第 20 个位置开始查找
// 结果: int(27)
var_dump(strrpos($foo, '7', 28)); // 结果: bool(false)
?>
子串
substr()
substr()返回字符串的子串
string substr ( string $string , int $start [, int $length ] )
<?php
$rest = substr("abcdef", -1); // 返回 "f"
$rest = substr("abcdef", -2); // 返回 "ef"
$rest = substr("abcdef", -3, 1); // 返回 "d"
?>
<?php
$rest = substr("abcdef", 0, -1); // 返回 "abcde"
$rest = substr("abcdef", 2, -1); // 返回 "cde"
$rest = substr("abcdef", 4, -4); // 返回 ""
$rest = substr("abcdef", -3, -1); // 返回 "de"
?>
替换
str_replace()
str_replace()返回一个字符串或者数组。该字符串或数组是将 subject 中全部的 search 都被 replace 替换之后的结果
mixed str_replace ( mixed $search , mixed $replace , mixed $subject [, int &$count ] )
如果count被指定,它的值将被设置为替换发生的次数
<?php
// 赋值: <body text='black'>
$bodytag = str_replace("%body%", "black", "<body text='%body%'>");
// 赋值: Hll Wrld f PHP
$vowels = array("a", "e", "i", "o", "u", "A", "E", "I", "O", "U");
$onlyconsonants = str_replace($vowels, "", "Hello World of PHP");
// 赋值: You should eat pizza, beer, and ice cream every day
$phrase = "You should eat fruits, vegetables, and fiber every day.";
$healthy = array("fruits", "vegetables", "fiber");
$yummy = array("pizza", "beer", "ice cream");
$newphrase = str_replace($healthy, $yummy, $phrase);
// 赋值: 2
$str = str_replace("ll", "", "good golly miss molly!", $count);
echo $count;
?>
分割
【explode()】
explode()使用一个字符串分割另一个字符串,返回由字符串组成的数组,每个元素都是string的一个子串,它们被字符串delimiter作为边界点分割出来
array explode ( string $delimiter , st