在开发有preg_replace preg_grep preg_split preg_replace_callback preg_quote进行一些必要操作,php实例代码如下:
- $temp[0]="abc";
- $temp[1]=123;
- $temp[2]="us.";
- $temp[3]=5;
- $temp[4]=58;
- $temp[5]="cs";
-
- echo "数组temp的内容为:";
- echo "<p>";
-
- for($i=0;$i<count($temp);$i++)
- {
- echo $temp[$i];
- echo ",";
- }
- echo "<p>";
- echo "经过preg_grep()处理过之后的新数组为:";
- $temp2=preg_grep("/^(d)d*/",$temp);
-
- echo "<p>";
- print_r($temp2);
-
-
-
- $str='string';
- $chars=preg_split('//',$str,-1,preg_split_no_empty); //用正则表达式执行分割操作
- print_r($chars);
-
-
-
-
- $string="april 15, 2003";
- $pattern="/(w+)(d+),(d+)/i";
- $replacement="${1}1,$3";
- print preg_replace($pattern,$replacement,$string);
-
-
-
- $text="april fools day is 04/01/2002n";
- $text.="last christmas was 12/24/2001n";
- $pattern="|(d{2}/d{2}/)(d{4})|";
- function next_year($matches)
- {
- return $matches[1].($matches[2]+1);
- }
- $result=preg_replace_callback($pattern,"next_year",$text);
- echo $result;
-
-
- $keywords="'php'";
- $keywords=preg_quote($keywords,"'");
- echo $keywords;
|