replace string
mixed preg_replace ( mixed $pattern , mixed $replacement , mixed $subject [, int $limit = -1 [, int &$count ]] )
mixed preg_filter ( mixed $pattern , mixed $replacement , mixed $subject [, int $limit = -1 [, int &$count ]] )
The difference between the two functions:
<?php
$subject = array('1', 'a', '2', 'b', '3', 'A', 'B', '4');
$pattern = array('/\d/', '/[a-z]/', '/[1a]/');
$replace = array('A:$0', 'B:$0', 'C:$0');
echo "preg_filter returns\n";
print_r(preg_filter($pattern, $replace, $subject));
echo "preg_replace returns\n";
print_r(preg_replace($pattern, $replace, $subject));
result:
preg_filter returns
Array
(
[0] => A:C:1
[1] => B:C:a
[2] => A:2
[3] => B:b
[4] => A:3
[7] => A:4
)
preg_replace returns
Array
(
[0] => A:C:1
[1] => B:C:a
[2] => A:2
[3] => B:b
[4] => A:3
[5] => A
[6] => B
[7] => A:4
)
preg_filter() is equivalent to preg_replace(), except that preg_filter only returns the replaced array elements.
match string
int preg_match ( string $pattern , string $subject [, array &$matches [, int $flags = 0 [, int $offset = 0 ]]] )
int preg_match_all ( string $pattern , string $subject [, array &$matches [, int $flags = PREG_PATTERN_ORDER [, int $offset = 0 ]]] )
preg_match()
The search will stop after the first match.preg_match_all()
Unlike this, it searches subject until it reaches the end. If an error occurs, both returnFALSE
.
Returns an array of matching patterns
array preg_grep ( string $pattern , array $input [, int $flags = 0 ] )
example:
<?php
$subject = array(34,23.34,3445,54.23);
// 返回所有包含浮点数的元素
$fl_array = preg_grep("/^(\d+)?\.\d+$/", $subject);
var_dump($fl_array);
result:
Returns an array indexed by key in input.
callback replacement
function:preg_replace_callback
mixed preg_replace_callback ( mixed $pattern , callable $callback , mixed $subject [, int $limit = -1 [, int &$count ]] )
example:
<?php
// 将文本中的年份增加一年.
$text = "April fools day is 04/01/2002\n";
$text.= "Last christmas was 12/24/2001\n";
// 回调函数
function next_year($matches)
{
// 通常: $matches[0]是完成的匹配
// $matches[1]是第一个捕获子组的匹配
// 以此类推
return $matches[1].($matches[2]+1);
}
echo preg_replace_callback(
"/(\d{2}\/\d{2}\/)(\d{4})/",
"next_year",
$text);
result:
April fools day is 04/01/2003
Last christmas was 12/24/2002
function:preg_replace_callback_array
mixed preg_replace_callback_array ( array $patterns_and_callbacks , mixed $subject [, int $limit = -1 [, int &$count ]] )
example:
<?php
$subject = 'Aaaaaa Bbb';
preg_replace_callback_array(
[
'/[a]+/i' => function ($match) {
echo strlen($match[0]), ' matches for "a" found', PHP_EOL;
},
'/[b]+/i' => function ($match) {
echo strlen($match[0]), ' matches for "b" found', PHP_EOL;
}
],
$subject
);
result
6 matches for "a" found
3 matches for "b" found
split string
array preg_split ( string $pattern , string $subject [, int $limit = -1 [, int $flags = 0 ]] )
example:
<?php
//使用逗号或空格(包含" ", \r, \t, \n, \f)分隔短语
$keywords = preg_split("/[\s,]+/", "hypertext language, programming");
print_r($keywords);
result:
Array
(
[0] => hypertext
[1] => language
[2] => programming
)