正则表达式,正则表达式基础整理!返回列表
上传时间:2015-02-02 内容关键字:正则表达式,正则表达式基础整理!
例子:
1. $search="函数";
$replace=''.$search.'';
$newstr=str_replace($search, $replace, $str);
2. $search=array("lamp", "Apache", "Linux", "MySQL", "Php");
$replace="**";
$newstr=str_ireplace($search, $replace, $str, $count);
3. $search=array("lamp", "Apache", "Linux", "MySQL", "Php");
$replace=array("J2EE", "Tomcat", "Windows", "Oracle", "Jsp");
$newstr=str_ireplace($search, $replace, $str, $count);
function setUrl($str) {
$url="/(https?|ftps?):\/\/((www|mail|news)\.([^\.\/]+)\.(com|org|net))/i";
preg_match_all($url, $str, $arr,PREG_PATTERN_ORDER );
$str=str_replace($arr[0], array_map("addLink", $arr[0]), $str);
return $str;
}
function addLink($url){
return ''.$url.'';
}
echo setUrl($str);
2. preg_replace()
四种用法:
1. 正常使用 preg_replace('string', 'string', 'stirng',int);
2. 在正则中的子模式,可以用到二个参数中
3. 在第二个参数中调用函数, 需要在模式中使用 e 模式修正符号
4. 就是在前两个参数中都使用数组, 可以一起将多个模式(正则)同时替换成多个值的形式
5. mixed preg_replace ( mixed pattern, mixed replacement, mixed subject [, int limit] )
在 subject 中搜索 pattern 模式的匹配项并替换为 replacement。如果指定了 limit,则仅替换 limit 个匹配,如果省略 limit 或者其值为 -1,则所有的匹配项都会被替换。
replacement 可以包含 \\n 形式或(自 PHP 4.0.4 起)$n 形式的逆向引用,首选使用后者。 每个此种引用将被替换为与第
- 上一篇:php-fpm 基础介绍
- 下一篇:什么是面向对象?面向对象的简单了解!