常见字符串和数组函数
range
<?php // array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12) foreach (range(0, 12) as $number) { echo $number; } // The step parameter was introduced in 5.0.0 // array(0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100) foreach (range(0, 100, 10) as $number) { echo $number; } // Use of character sequences introduced in 4.1.0 // array(‘a‘, ‘b‘, ‘c‘, ‘d‘, ‘e‘, ‘f‘, ‘g‘, ‘h‘, ‘i‘); foreach (range(‘a‘, ‘i‘) as $letter) { echo $letter; } // array(‘c‘, ‘b‘, ‘a‘); foreach (range(‘c‘, ‘a‘) as $letter) { echo $letter; } ?>
shuffle
<?php $numbers = range(1, 20); shuffle($numbers); foreach ($numbers as $number) { echo "$number "; } ?>
rand
rand() 和 mt_rand() 都是闭区间
Note: 自 PHP 4.2.0 起,不再需要用 srand() 或 mt_srand() 给随机数发生器播种 ,因为现在是由系统自动完成的
<?php echo mt_rand() . "\n"; echo mt_rand() . "\n"; echo mt_rand(5, 15); ?>
// 计算一个随机浮点数 <?php function randomFloat($min = 0, $max = 1) { return $min + mt_rand() / mt_getrandmax() * ($max - $min); } var_dump(randomFloat()); var_dump(randomFloat(2, 20)); ?>
join
别名: implode()
# 将数组变成字符串 # 返回的是字符串 string implode ( string $glue , array $pieces ) string implode ( array $pieces )
注意空白数组不能使用 join
<?php $array = array(‘lastname‘, ‘email‘, ‘phone‘); $str = join(", ", $array); echo $str.‘‘; // lastname,email,phone // Empty string when using an empty array: var_dump(join(‘hello‘, array())); // string(0) "" ?>
各种字符串分割的比较
preg_split() 函数使用了 Perl 兼容正则表达式语法,通常是比 split() 更快的替代方案。如果不需要正则表达式的威力,则使用 explode() 更快,这样就不会招致正则表达式引擎的浪费
explode
array explode ( string $delimiter, string $string [, int $limit ])
# 将字符串变成数组
<?php // 示例 1 $pizza = "piece1 piece2 piece3 piece4 piece5 piece6"; $pieces = explode(" ", $pizza); echo $pieces[0]; // piece1 echo $pieces[1]; // piece2 // 示例 2 $data = "foo:*:1023:1000::/home/foo:/bin/sh"; list($user, $pass, $uid, $gid, $gecos, $home, $shell) = explode(":", $data); echo $user; // foo echo $pass; // * ?>
<?php /* A string that doesn‘t contain the delimiter will simply return a one-length array of the original string. */ // 如果字符串不存在该分隔符, 那么整个字符串就会被 合并成 一个 数组 $input1 = "hello"; $input2 = "hello,there"; var_dump( explode( ‘,‘, $input1 ) ); var_dump( explode( ‘,‘, $input2 ) ); ?>
<?php $str = ‘one|two|three|four‘; // 正数的 limit print_r(explode(‘|‘, $str, 2)); // 负数的 limit(自 PHP 5.1 起) print_r(explode(‘|‘, $str, -1)); // 结果 // Array // ( // [0] => one // [1] => two|three|four // ) // Array // ( // [0] => one // [1] => two // [2] => three // ) ?>
split
split — 用正则表达式将字符串分割到数组中
array split (string $pattern , string $string [, int $limit ])
<?php $passwd_line = "foo:*:1023:1000::/home/foo:/bin/sh"; list($user, $pass, $uid, $gid, $extra) = split (":", $passwd_line); echo ‘|‘, $passwd_line, ‘|<br />‘; echo ‘|‘, $user, ‘|‘, $pass, ‘|‘, $uid, ‘|‘, $gid, ‘|‘, $extra, ‘|<br />‘; echo ‘<br />‘; list($user, $pass, $uid, $gid, $extra) = split (":", $passwd_line, 5); echo ‘|‘, $passwd_line, ‘|<br />‘; echo ‘|‘, $user, ‘|‘, $pass, ‘|‘, $uid, ‘|‘, $gid, ‘|‘, $extra, ‘|<br />‘; echo ‘<br />‘; // 分隔符可以是斜线,点,或横线 $date = "04/30/1973"; list($month, $day, $year) = split (‘[/.-]‘, $date); echo "Month: $month; Day: $day; Year: $year <br />"; ?>
prep_split
preg_split — 通过一个正则表达式分隔字符串
array preg_split ( string $pattern , string $subject [, int $limit = -1 [, int $flags = 0 ]] )
flags PREG_SPLIT_NO_EMPTY preg_split() 将进返回分隔后的非空部分 PREG_SPLIT_DELIM_CAPTURE 用于分隔的模式中的括号表达式将被捕获并返回 PREG_SPLIT_OFFSET_CAPTURE 对于每一个出现的匹配返回时将会附加字符串偏移量. 注意:这将会改变返回数组中的每一个元素, 使其每个元素成为一个由第 0 个元素为分隔后的子串,第 1 个元素为该子串在 subject 中的偏移量组成的数组
<?php // 使用逗号或空格(包含" ", \r, \t, \n, \f)分隔短语 $keywords = preg_split("/[\s,]+/", "hypertext language, programming"); print_r($keywords); // Array ( [0] => hypertext [1] => language [2] => programming ) ?>
<?php $str = ‘str ing‘; $chars = preg_split(‘//‘, $str, -1, PREG_SPLIT_NO_EMPTY); print_r($chars); // Array ( [0] => s [1] => t [2] => r [3] => [4] => i [5] => n [6] => g ) ?>
<?php $str = ‘hypertext language programming‘; $chars = preg_split(‘/ /‘, $str, -1, PREG_SPLIT_OFFSET_CAPTURE); print_r($chars); // Array ( // [0] => Array ( [0] => hypertext [1] => 0 ) // [1] => Array ( [0] => language [1] => 10 ) // [2] => Array ( [0] => programming [1] => 19 ) // ) ?>
str_split
array str_split ( string $string [, int $split_length = 1 ] )
str_split — 将字符串转换为数组
<?php $str = "Hello Friend"; $arr1 = str_split($str); $arr2 = str_split($str, 3); print_r($arr1); print_r($arr2); ?> // Array // ( // [0] => H // [1] => e // [2] => l // [3] => l // [4] => o // [5] => // [6] => F // [7] => r // [8] => i // [9] => e // [10] => n // [11] => d // ) // Array // ( // [0] => Hel // [1] => lo // [2] => Fri // [3] => end // )