PHP正则表达式

  • preg_grep($pattern, $input[, $flag = 0]):返回匹配模式的数组
    参数:
    $pattern:正则表达式,要搜索的模式
    $input:输入的数组
    $flag:当设置为PREG_GREP_INVERT,则搜索与指定模式pattern不匹配的数组元素
<?php
$arr = [1,2,3.4,5,6,7.8,9];
$preg = preg_grep("/^(\d+)?\.\d+$/",$arr);  //返回所有包含浮点数的元素
print_r($preg);								//Array ( [2] => 3.4 [5] => 7.8 )
  • preg_match($pattern, $subject[, &$matches[, $flags = 0[, $offset = 0]]]):搜索 subject 与 pattern 给定的正则表达式的一个匹配
    参数:
    $pattern:正则表达式,要搜索的模式
    $subject:输入的字符串
    $matches:当提供了改参数时,将会填充匹配结果,$matches[0]将包含完整模式匹配到的文本, $matches[1] 将包含第一个捕获子组匹配到的文本,以此类推
    $flag:当设置为PREG_OFFSET_CAPTURE时,对于每一个出现的匹配返回时会附加字符串偏移量
    $offset:从目标字符串的某个位置开始
    返回值:
    返回pattern的匹配次数,0或1次,发生错误则返回false
/* 模式中的\b标记一个单词边界,所以只有独立的单词"web"会被匹配,而不会匹配单词的部分内容比如"webbing" 或 "cobweb" */
if (preg_match("/\bweb\b/i", "PHP is the web scripting language of choice.")) {
    echo "查找到匹配的字符串。\n";  		//查找到匹配的字符串。
} else {
    echo "未发现匹配的字符串。\n";
}
// 从URL中获取主机名称
preg_match("/^(?:http:\/\/)?([^\/]+)/i","http://www.yyfs.com/index.html", $matches);
var_dump($matches);	// array(2) { [0]=> string(19) "http://www.yyfs.com" [1]=> string(12) "www.yyfs.com" }
// 从URL中获取主机名称
preg_match("/^(?:http:\/\/)?([^\/]+)/i","http://www.yyfs.com/index.html", $matches,PREG_OFFSET_CAPTURE);
echo '<pre>';
print_r($matches);
echo '</pre>';
/*
Array
(
    [0] => Array
        (
            [0] => http://www.yyfs.com
            [1] => 0
        )
    [1] => Array
        (
            [0] => www.yyfs.com
            [1] => 7
        )
)
*/
  • preg_match_all($pattern, $subject[, &$matches[, $flags = 0[, $offset = 0]]]):搜索 subject 与 pattern 给定的正则表达式的一个匹配
    参数:与preg_match基本类似
    $flag:
       PREG_PATTERN_ORDER:结果排序为$matches[0]保存完整模式的所有匹配, $matches[1] 保存第一个子组的所有匹配,以此类推
       PREG_SET_ORDER:结果排序为$matches[0]包含第一次匹配得到的所有匹配(包含子组), $matches[1]是包含第二次匹配到的所有匹配(包含子组)的数组,以此类推
       PREG_OFFSET_CAPTURE:对于每一个出现的匹配返回时会附加字符串偏移量
    返回值:
    返回pattern的匹配次数,0或n次,发生错误则返回false
$userinfo = "Name: <b>PHP</b> <br> Title: <b>Programming Language</b>";
preg_match_all ("/<b>(.*)<\/b>/U", $userinfo, $pat_array);
print_r($pat_array[0]);    //Array ( [0] => PHP [1] => Programming Language )
// 从URL中获取主机名称
preg_match("/^(?:http:\/\/)?([^\/]+)/i","http://www.yyfs.com/index.html", $matches);
var_dump($matches);	// array(2) { [0]=> string(19) "http://www.yyfs.com" [1]=> string(12) "www.yyfs.com" }
//\\2是一个后向引用的示例. 这会告诉pcre它必须匹配正则表达式中第二个圆括号(这里是([\w]+))
//匹配到的结果. 这里使用两个反斜线是因为这里使用了双引号.
$html = "<b>bold text</b><a href=howdy.html>click me</a>";
preg_match_all("/(<([\w]+)[^>]*>)(.*?)(<\/\\2>)/", $html, $matches, PREG_SET_ORDER);
var_dump($matches);
/*
array(2) {
  [0]=>
  array(5) {
    [0]=>
    string(16) "<b>bold text</b>"
    [1]=>
    string(3) "<b>"
    [2]=>
    string(1) "b"
    [3]=>
    string(9) "bold text"
    [4]=>
    string(4) "</b>"
  }
  [1]=>
  array(5) {
    [0]=>
    string(31) "<a href=howdy.html>click me</a>"
    [1]=>
    string(19) "<a href=howdy.html>"
    [2]=>
    string(1) "a"
    [3]=>
    string(8) "click me"
    [4]=>
    string(4) "</a>"
  }
}
*/
  • preg_split($pattern, $subject[, $limit = -1[, $flags = 0]]):搜索 subject 与 pattern 给定的正则表达式的一个匹配
    参数:
    $pattern:正则表达式,要搜索的模式
    $subject:输入的字符串
    $limit:如果指定将限制分隔得到的子串最多只有limit个,最后的子串将包含剩余的所有部分,limit=-1/0/null时都代表不限制
    $flag:PREG_SPLIT_NO_EMPTY:将返回分隔后的非空部分
       PREG_SPLIT_DELIM_CAPTURE:用于分隔的模式中的括号表达式将被捕获并返回
       PREG_OFFSET_CAPTURE:对于每一个出现的匹配返回时会附加字符串偏移量
    返回值:
    返回一个使用 pattern 边界分隔 subject 后得到的子串组成的数组
$keywords = preg_split("/([\s,]+)/", "hypertext language, programming",-1,PREG_SPLIT_DELIM_CAPTURE);
print_r($keywords);
/*
Array
(
    [0] => hypertext
    [1] =>  			//分隔符
    [2] => language
    [3] => , 			//分隔符
    [4] => programming
)
*/
  • preg_replace($pattern, $replacement, $subject[, $limit = -1[, &$count]]):搜索 subject 与 pattern 给定的正则表达式的部分,用replacement替换
    参数:
    $pattern:正则表达式,要搜索的模式
    $replacement:替换的字符串
    $subject:输入的字符串
    $limit:最大可替换数,-1 代表不限制
    $count:替换执行的次数
    返回值:
    当subject是一个数组时,返回一个匹配的数组,其他情况返回字符串,匹配错误返回null
$str = 'yy f s  ';
$str = preg_replace('/\s+/', '', $str);
echo $str;			//yyfs
  • preg_quote($str, $replacementt[, &$delimiter = NULL]):转义正则表达式字符
    参数:
    $str:输入的字符串
    $delimiter:如果指定了可选参数 delimiter,它也会被转义
    返回值:
    返回转义后的字符串
//preg_quote($word):用于保持星号原文涵义,使其不使用正则表达式中的特殊语义。
$textbody = "This book is *very* difficult to find.";
$word = "*very*";
$textbody = preg_replace ("/" . preg_quote($word) . "/", "<i>" . $word . "</i>", $textbody);
echo $textbody;  //This book is <i>*very*</i> difficult to find.
  • preg_replace_callback($pattern, $callback, $subject[, $limit = -1[, &$count]]):执行一个正则表达式搜索并且使用一个回调进行替换
    参数:
    $pattern:正则表达式,要搜索的模式
    $callback:在每次需要替换时调用回调函数
    $subject:输入的字符串
    $limit:最大可替换数,-1 代表不限制
    $count:替换执行的次数
    返回值:
    当subject是一个数组时,返回一个匹配的数组,其他情况返回字符串,匹配错误返回null
// 将文本中的年份增加一年.
$text = "April fools day is 04/01/2002\n";
$text.= "Last christmas was 12/24/2001\n";
// 回调函数
function next_year($matches)
{
  // 通常: $matches[0]是完成的匹配
  // $matches[1]是第一个捕获子组的匹配
  // 以此类推
  return $matches[1].($matches[2]+1);
}
echo preg_replace_callback("|(\d{2}/\d{2}/)(\d{4})|", "next_year", $text);
//April fools day is 04/01/2003 Last christmas was 12/24/2002 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值