PHP的正则
2015-08-04 02:06
609 查看
之前看过很多遍正则30钟,然而并没有练习,所以一点进步都没有,现在来总结一下这个晚上我看的吧…..
就是这些啦,只是把PCRE函数库里面的函数都过了一遍,然而正则其他的一些细节还没仔细看看,现在就先贴在这儿吧,有空再来看看
<?php /** * preg_filter() * @var string */ // $subject = 'njks41854415ddf'; // $pattern = '/5/'; // $replace = 'ba'; // echo "preg_filter returns\n"; // print_r(preg_filter($pattern, $replace, $subject)); // echo "\n"; // echo "preg_replace returns\n"; // print_r(preg_replace($pattern, $replace, $subject)); /** * preg_grep() 仅用于数组 * @var array */ // $subjects = array("Mechanical Engineering", "Medicine", "Social Science", "Agriculture", "Commercial Science", "Politics" ); // //匹配所有仅由有一个单词组成的科目名 // $alonewords = preg_grep("/^[a-z]*$/i", $subjects); // var_dump($alonewords); /** * preg_last_error() 返回错误代码 */ // preg_match('/(?:\D+|<\d+>)*[!?]/', 'foobar foobar foobar'); // if (preg_last_error() == PREG_BACKTRACK_LIMIT_ERROR) { // print 'Backtrack limit was exhausted!'; // } /** * preg_match_all() 执行一个全局正则表达式的匹配 * PREG_PATTERN_ORDER和PREG_SET_ORDER不可同时出现 * */ //PREG_PATTERN_ORDER模式 /*preg_match_all("|<[^>]+>(.*)</[^>]+>|U", "<b>example: </b><div align=left>this is a test</div>", $out, PREG_PATTERN_ORDER); echo $out[0][0] . ", " . $out[0][1] . "\n"; echo $out[1][0] . ", " . $out[1][1] . "\n";*/ /* 结果:<b>example: </b>, <div align=left>this is a test</div> example: , this is a test*/ //PREG_SET_ORDER模式 /*preg_match_all("|<[^>]+>(.*)</[^>]+>|U", "<b>example: </b><div align=\"left\">this is a test</div>", $out, PREG_SET_ORDER); echo $out[0][0] . ", " . $out[0][1] . "\n"; echo $out[1][0] . ", " . $out[1][1] . "\n";*/ /*结果:<b>example: </b>, example: <div align="left">this is a test</div>, this is a test*/ /** * preg_match() 只搜索0/1次 */ /*$subject = "abcdef"; $pattern = '/^def/'; preg_match($pattern, $subject, $matches, PREG_OFFSET_CAPTURE, 3); print_r($matches); //啥也匹配不了 $rest = substr("abcdef", 1); //从第二位开始 print_r($rest."\n"); $subject = "abcdef"; $pattern = '/^def/'; preg_match($pattern, substr($subject,3), $matches, PREG_OFFSET_CAPTURE); //成功匹配到def print_r($matches);*/ /*$textbody = "This book is *very* difficult to find."; $word = "*very*"; $textbody = preg_replace ("/" . preg_quote($word) . "/", "<i>" . $word . "</i>", $textbody); //preg_quote() 保持星号原含义 print_r($textbody); */ /** * preg_replace_callback(); * @var string */ $input = "plain [indent] deep [indent] deeper [/indent] deep [/indent] plain"; function parseTagsRecursive($input) { /* 译注: 对此正则表达式分段分析 * 首尾两个#是正则分隔符 * \[indent] 匹配一个原文的[indent] * ((?:[^[]|\[(?!/?indent])|(?R))+)分析: * (?:[^[]|\[(?!/?indent])分析: * 首先它是一个非捕获子组 * 两个可选路径, 一个是非[字符, 另一个是[字符但后面紧跟着不是/indent或indent. * (?R) 正则表达式递归 * \[/indent] 匹配结束的[/indent] */ /* $regex = '#\[indent]((?:[^[]|\[(?!/?indent])|(?R))+)\[/indent]#'; if (is_array($input)) { $input = '<div style="margin-left: 10px">'.$input[1].'</div>'; } return preg_replace_callback($regex, 'parseTagsRecursive', $input); } $output = parseTagsRecursive($input); echo $output;*/ /** * preg_split() 通过一个正则表达式分隔字符串 */ //使用逗号或空格(包含" ", \r, \t, \n, \f)分隔短语 $keywords = preg_split("/[\s,]+/", "hypertext language, programming"); print_r($keywords); //结果:Array ( [0] => hypertext [1] => language [2] => programming )
就是这些啦,只是把PCRE函数库里面的函数都过了一遍,然而正则其他的一些细节还没仔细看看,现在就先贴在这儿吧,有空再来看看
相关文章推荐
- 一个关于if else容易迷惑的问题
- PHP5.2.*防止Hash冲突拒绝服务攻击的Patch
- 深入理解PHP之匿名函数
- JSP/PHP基于Ajax的分页功能实现
- 关于PHP通过PDO用中文条件查询MySQL的问题。
- 什么是设计模式
- Java简单字符串插值实现
- PHP数据库长连接mysql_pconnect的细节
- Php Installing An Expansion
- PHP+Apache在Windows 9x下的安装和配置
- IIS 6 的 PHP 最佳配置方法
- 安装Apache和PHP的一些补充
- Linux Apache+MySQL+PHP
- 建立Apache+PHP+MySQL数据库驱动的动态网站
- PHP 5.3.0 安装分析心得
- apache 环境下 php 的配置注意事项
- C# 正则判断一个数字的格式是否有逗号的代码
- C#常用正则大全分享
- ASP.NET、ASP、PHP、JSP之间有什么区别?