PHP的正则

之前看过很多遍正则30钟,然而并没有练习,所以一点进步都没有,现在来总结一下这个晚上我看的吧…..

<?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函数库里面的函数都过了一遍,然而正则其他的一些细节还没仔细看看,现在就先贴在这儿吧,有空再来看看

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值