php正则表达式

php中preg_match正则匹配的开关项

/u 表示按unicode(utf-8)匹配,主要针对多字节比如汉字

/i 表示不区分大小写

/s 表示将字符串视为单行来处理

int preg_match_allstring $pattern string$subject [, array&$matches [, int $flags PREG_PATTERN_ORDER [, int $offset = 0 ]]] )

搜索subject中所有匹配pattern给定正则表达式的匹配结果并且将它们以flag指定顺序输出到matches中。在第一个匹配找到后,子序列继续从最后一次匹配位置搜索。

参数:pattern 要搜索的模式,字符串形式

subject 输入字符串

matches 多维数组,作为输出参数输出所有匹配结果,数组排序通过flags指定

返回值:返回完整匹配次数(可能是0),或者如果发生错误返回FALSE

结果排序为 $matches[0]保存完整模式的所有匹配,$matches[1]保存第一个子组的所有匹配,依次类推。

<span style="font-size:10px;">$str='<b>example: </b><div align=left>this is a test</div>';
$pattern='|<[^>]+>(.*)</[^>]+>|U';
preg_match_all($pattern,$str,$matches);
dd($matches);</span>

$str='<b>example: </b><div align=left>this is a test</div>';
    $pattern='/<[^>]+>(.*)<\/[^>]+>/U';//需加转义符号\/
    preg_match_all($pattern,$str,$matches);
    dd($matches);

结果同上

<span style="font-size:10px;">$str='<b>example: </b><div align=left>this is a test</div>';
    $pattern='/<[^>]+>(.*)<\/[^>]+>/';//需加转义符号\/
    preg_match_all($pattern,$str,$matches);
    dd($matches);</span>
注意:没有/U


替换匹配正则表达式的子串
替换匹配正则表达式的子串,使用preg_replace()替换,并没有实现要想的效果,我就试着用preg_split()先用正则表达式进行分割,然后再进行拼接实现。
    $str='<text format="html">为降低大城市震后救灾活动强度,应采取的主要防灾减灾措施包括__<label id="9_1">完善城市功能区划 </label>   <label id="9_2">调整产业结构</label><label id="9_3">人口外迁</label>    </text>';
    $pattern='/<label[^>]+>(.*)<\/label>/U';
    preg_match_all($pattern,$str,$matches);
    //dd($matches);
    $arr_split=preg_split($pattern,$str);
    dd($arr_split);
被正则表达式分割之后的子串:

匹配正则表达式的子串

拼接字符串

   $str='<text format="html">为降低大城市震后救灾活动强度,应采取的主要防灾减灾措施包括__<label id="9_1">完善城市功能区划 </label>   <label id="9_2">调整产业结构</label><label id="9_3">人口外迁</label>    </text>';
    $pattern='/<label[^>]+>(.*)<\/label>/U';
    preg_match_all($pattern,$str,$matches);
    // dd($matches);
    $arr_split=preg_split($pattern,$str);
    // dd($arr_split);
    $arr_mark=array();
    $arr_mark[0]="①";
    $arr_mark[1]="②";
    $arr_mark[2]="③";
    $arr_mark[3]="④";
    $arr_mark[4]="⑤";
    $arr_mark[5]="⑥";
    $arr_mark[6]="⑦";
    $arr_mark[7]="⑧";
    $arr_mark[8]="⑨";
    $count=count($matches[1]);
    $str_new="";
    for($i=0;$i<$count;$i++){
        $str_new.=$arr_split[$i];
        $str_new.=$arr_mark[$i];
        $str_new.=$matches[1][$i];
    }
    $str_new.=$arr_split[$count];
    echo $str_new;
    dd($str_new);




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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值