PHP100 2019-03-27
代码如下:
<?php /* *模式分隔符后的"i"标记这是一个大小写不敏感的搜索 *将会输出:1 */ echo preg_match("/,\s*(php)/i", "In my point, PHP is the web scripting language of choice."); echo "<br/>"."\n"; /* *将会输出:Array([0]=>, PHP [1]=>PHP) */ $matches = array(); preg_match("/,\s*(php)/i", "In my point, PHP is the web scripting language of choice. I love php", $matches); print_r($matches); echo "<br/>"."\n"; /* *将会输出:Array([0]=>Array([0]=>, PHP [1]=>11) [1]=>Array([0]=>PHP [1]=>13)) */ preg_match("/,\s*(php)/i", "In my point, PHP is the web scripting language of choice. I love php", $matches, PREG_OFFSET_CAPTURE); print_r($matches); echo "<br/>"."\n"; /* *将会输出:Array([0]=>Array([0]=>e php [1]=63) [1]=>Array([0]=>php [1]=>65)) */ preg_match("/[,a-z]?\s*(php)/i", "In my point, PHP is the web scripting language of choice. I love php", $matches, PREG_OFFSET_CAPTURE, 28); print_r($matches); echo "<br/>"."\n"; ?>
代码如下:
<?php /* *将会输出:2 */ echo preg_match_all("/php/i", "In my point, PHP is the web scripting language of choice. I love php", $matches); echo "<br/>"."\n"; /* *将会输出:Array([0]=>, PHP [1]=>PHP) */ $matches = array(); preg_match("/[,a-z]?\s*(php)/i", "In my point, PHP is the web scripting language of choice. I love php", $matches); print_r($matches); echo "<br/>"."\n"; /* *将会输出:Array([0]=>Array([0]=>, PHP [1]=>e php) [1]=>Array([0]=>PHP [1]=>php)) */ $matches = array(); preg_match_all("/[,a-z]?\s*(php)/i", "In my point, PHP is the web scripting language of choice. I love php", $matches, PREG_PATTERN_ORDER); print_r($matches); echo "<br/>"."\n"; /* *将会输出:Array([0]=>Array([0]=>Array([0]=>, PHP [1]=>11) [1]=>Array([0]=>PHP [1]=>13)) [1]=>Array([0]=>Array([0]=>e php [1]=>63) [1]=>Array([0]=>php [1]=>65))) */ $matches = array(); preg_match_all("/[,a-z]?\s*(php)/i", "In my point, PHP is the web scripting language of choice. I love php", $matches, PREG_SET_ORDER|PREG_OFFSET_CAPTURE); print_r($matches); echo "<br/>"."\n"; /* *Array([0]=>Array([0]=>e php [1]=>63) [1]=>Array([0]=>php [1]=>65)) */ $matches = array(); preg_match_all("/[,a-z]?\s*(php)/i", "In my point, PHP is the web scripting language of choice. I love php", $matches, PREG_SET_ORDER|PREG_OFFSET_CAPTURE, 28); print_r($matches); echo "<br/>"."\n"; ?>
代码如下:
<?php /* *将会输出: *Array ( [0] => In my point, [1] => is the web scripting language of choice. I love [2] => ) */ $matches = array(); print_r(preg_split("/php/i", "In my point, PHP is the web scripting language of choice. I love php")); echo "<br/>"."\n"; /* *将会输出: *Array ( [0] => In my point, [1] => is the web scripting language of choice. I love php ) */ $matches = array(); print_r(preg_split("/php/i", "In my point, PHP is the web scripting language of choice. I love php", 2)); echo "<br/>"."\n"; /* *将会输出: *Array ( [0] => In my point, [1] => is the web scripting language of choice. I love ) */ $matches = array(); print_r(preg_split("/php/i", "In my point, PHP is the web scripting language of choice. I love php", -1, PREG_SPLIT_NO_EMPTY)); echo "<br/>"."\n"; ?>
代码如下:
<?php //在这个例子中,preg_quote($word) 用于保持星号原文涵义,使其不使用正则表达式中的特殊语义。 $textbody = "This book is *very* difficult to find."; $word = "*very*"; $textbody = preg_replace ("/" . preg_quote($word) . "/", "<i>" . $word . "</i>", $textbody); //将会输出This book is <i>*very*</i> difficult to find. echo htmlspecialchars($textbody); ?>
代码如下:
<?php $array = array("abc", "dd", "123", "123.22", "word123", "33.2", "0.22"); //返回所有包含浮点数的元素 //输出:Array ( [3] => 123.22 [5] => 33.2 [6] => 0.22 ) $fl_array = preg_grep("/^(\d+)?\.\d+$/", $array); print_r($fl_array); //返回所有包含浮点数的元素 //输出:Array ( [0] => abc [1] => dd [2] => 123 [4] => word123 ) $fl_array = preg_grep("/^(\d+)?\.\d+$/", $array, PREG_GREP_INVERT); print_r($fl_array); ?>
代码如下:
<?php $string = 'April 15, 2003'; /* *\w+字符重复一次或者多次 *\d+数字重复一次或者多次 *i忽略大小写 */ $pattern = '/(\w+) (\d+), (\d+)/i'; /* *$0 完整的模式匹配文本 *${1}1 第一个小括号中的模式匹配文本并且在后面加1 *\\3 第三个小括号中的模式匹配文本 */ $replacement = '$0:<br/> ${1}1,\\3'; echo preg_replace($pattern, $replacement, $string); ?>
代码如下:
$string = 'The quick brown fox jumped over the lazy dog.'; $patterns = array(); $patterns[0] = '/quick/'; $patterns[1] = '/brown/'; $patterns[2] = '/fox/'; $replacements = array(); $replacements[2] = 'bear'; $replacements[1] = 'black'; $replacements[0] = 'slow'; //会输出:The bear black slow jumped over the lazy dog. echo preg_replace($patterns, $replacements, $string); //对模式和替换内容按key进行排序我们可以得到期望的结果. ksort($patterns); ksort($replacements); //会输出:The slow black bear jumped over the lazy dog. echo preg_replace($patterns, $replacements, $string);
代码如下:
<?php $patterns = array ('/(19|20)(\d{2})-(\d{1,2})-(\d{1,2})/', '/^\s*{(\w+)}\s*=/'); $replace = array ('\3/\4/\1\2', '$\1 ='); echo preg_replace($patterns, $replace, '{startDate} = 1999-5-27'); ?>
代码如下:
<?php $html_body = "<p><span>hello</span></p>"; //会输出:<P><SPAN>hello</SPAN></P> echo htmlspecialchars(preg_replace("/(<\/?)(\w+)([^>]*>)/e", "'\\1'.strtoupper('\\2').'\\3'", $html_body)); ?>
代码如下:
<?php $str = 'foo o'; $str = preg_replace('/\s\s+/', ' ', $str); // 将会改变为'foo o' echo $str; ?>
代码如下:
<?php $count = 0; echo preg_replace(array('/\d/', '/\s/'), '*', 'xp 4 to', -1 , $count); //等价于echo preg_replace('/\d|\s/', '', 'xp 4 to', -1 , $count); echo $count; //3 ?>
代码如下:
<?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); } /** *将会输出: *April fools day is 04/01/2003 *Last christmas was 12/24/2002 */ echo preg_replace_callback( "|(\d{2}/\d{2}/)(\d{4})|", "next_year", $text); //使用create_function echo preg_replace_callback( "|(\d{2}/\d{2}/)(\d{4})|", create_function( '$matches', 'return $matches[1].($matches[2]+1);' ), $text); ?>