<?php
/posix扩大正则表达式函数/
/---------------------------------------------------/
¥ereg = ^[¥][[:alpha:]_][[:alnum:]];
ereg(¥ereg,¥_name,¥register); //匹配并且存放数组 //eregi()不分大小写
var_dump(¥register); //显示数组布局
/---------------------------------------------------/
¥ereg = tm;
¥str = hello,tm,Tm,tM;
¥rep_str=eregi_replace(¥ereg,TM,¥str); //匹配成功的都用TM调换 ereg_replace() 分大小写
echo ¥rep_str;
/---------------------------------------------------/
¥ereg = is;
¥str = This is a register book.;
¥arr_str = spliti(¥ereg,¥str); //分别字符串。按正则匹配 split() 分大小写
var_dump(¥arr_str);
/pcre兼容正则表达式函数 --无论从效力或者语法上pcre都略优于posix/
/---------------------------------------------------/
¥preg = /\d{3,4}-?\d{7,8}/;
¥arr = array(043212345678,0431-7654321,12345678); //包含元素数组
¥preg_arr = preg_grep(¥preg,¥arr);
var_dump(¥preg_arr);
/---------------------------------------------------/
¥str = This is an example!;
¥preg = /\b\w{2}\b/;
¥num1 = preg_match(¥preg,¥str,¥str1); //匹配并返回成功个数,把匹配成功的放入数组 成功后,就停止
echo ¥num1.<br />;
var_dump(¥str1);
echo <br />;
//匹配并返回成功个数,把匹配成功的放入数组 成功后,不绝止,直到匹配所有字符停止
¥num2 = preg_match_all(¥preg,¥str,¥str2);
echo ¥num2.<br />;
var_dump(¥str2);
/---------------------------------------------------/
¥str = !、¥、^、、+、.、[、]、\\、/、b、<、>;
¥str2=b;
//主动将正则特别字符进行转义,若是有参数,字符中含参数字符也会被转义
¥match_one = preg_quote(¥str,¥str2);
echo ¥match_one;
/---------------------------------------------------/
¥string = [b]粗体字[/b];
//匹配并调换 用了反向引用
¥b_rst = preg_replace(/\[b\](.)\[\/b\]/i,<b>¥1</b>,¥string);
echo ¥b_rst;
/---------------------------------------------------/
function c_back(¥str)
{
//反向引用
¥str = <font color=¥str[1]>¥str[2]</font>;
return ¥str;
}
¥string = [color=blue]字体蓝色[/color];
//preg_replace_callback函数中要调换的字符,换成了回调函数罢了和 preg_replace差不久不多
echo preg_replace_callback(/\[color=(.)\](.)\[\/color\]/U,c_back,¥string);
/---------------------------------------------------/
//此函数书上没有,我本身写的示例给大师!
¥preg = /\s+/; //这里也可以换成 /\s{1,}/;
¥str = Im Chinese! I can eat;
¥arr = preg_split(¥preg,¥str); //调换
var_dump(¥arr);
?>