欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

PHP正則表達式函數(shù)preg_replace用法實例分析

 更新時間:2020年06月04日 09:44:52   作者:huangyuxin_  
這篇文章主要介紹了PHP正則表達式函數(shù)preg_replace用法,結合實例形式分析了PHP正則表達式函數(shù)preg_replace基本功能、參數(shù)描述與相關使用技巧,需要的朋友可以參考下

本文實例講述了PHP正則表達式函數(shù)preg_replace用法。分享給大家供大家參考,具體如下:

preg_replace 執(zhí)行一個正則表達式的搜索和替換

語法:preg_replace (pattern ,replacement ,subject,limit,count )

參數(shù) 描述
pattern 正則表達式(字符串或字符串數(shù)組)
replacement 用于替換的字符串或字符串數(shù)組
subject 要進行搜索和替換的字符串或字符串數(shù)組。
limit 可選。每個模式在每個subject上進行替換的最大次數(shù)。默認是 -1(無限)。
count 可選。完成的替換次數(shù)

Example 1

$string = 'huang yu xin';
$pattern = '/(\w+) (\w+) (\w+)/i';
$replacement = '${1}a $3';
// $1對應(\w+),${1}a是區(qū)別$1a,說明是$1和a不是$1a,$3對應第三個(\w+)
echo preg_replace($pattern, $replacement, $string);

結果是:

huanga xin

Example 2

$string = "nice to meet you";
$pattern = array();
$replace = array();
echo preg_replace(array('/nice/', '/you/'), array('Nice', 'me'), $string);

結果:

Nice to meet me

Example 3

$str = 'nice      !';
$str = preg_replace('/\s+/', '', $str);
echo $str;

結果:

nice!

Example 4

$count = 0;
echo preg_replace(array('/\d/', '/[a-z]/'), '*', 'xp 4 to', -1, $count);
echo $count;

結果:

** * **5

PS:這里再為大家提供2款非常方便的正則表達式工具供大家參考使用:

JavaScript正則表達式在線測試工具:
http://tools.jb51.net/regex/javascript

正則表達式在線生成工具:
http://tools.jb51.net/regex/create_reg

更多關于PHP相關內容感興趣的讀者可查看本站專題:《php正則表達式用法總結》、《php程序設計安全教程》、《php安全過濾技巧總結》、《PHP數(shù)組(Array)操作技巧大全》、《PHP基本語法入門教程》、《php字符串(string)用法總結》及《php+mysql數(shù)據庫操作入門教程

希望本文所述對大家PHP程序設計有所幫助。

相關文章

最新評論