preg_replace — 正则表达式匹配替换
preg_replace ( mixed $pattern , mixed $replacement , mixed $subje托物言志的文章ct [, int $limit = -1军训作文600字初一 [, int &$count ]] ) : mixed
搜索subject中符合pattern的部分,并用replacement替代。
replacement和pattern均可以是数组。
使用比较简单,功能和用法均类似于 str_replace.
比较难理解的是 replacement 可以后向引用。
replacement may contain references of the form \n or $n, with the latter form being the preferred one. every such reference will be replaced by the text captured by the n’th parenthesize广州市义务教育学校招生报名系统d pattern. n can be from 0 to 99, and \0 or $0 refers to the text matched by the whole pattern. opening parenthes are counted from left to right (starting from 1) to obtain the number of the capturing subpattern.信仰纪录片 note that backslashes in string literals may require to be escaped.
下面举个例子,说明下
<?php$string = 'april 15, 2003';$pattern = '/(\w+) (\d+), (\d+)/i';$replacement = '1,$3';// =april =15 =2003 \均可用$代替echo preg_replace($pattern, $replacement, $string);?>april1,2003
其中,双引号中 \ 和 $ 均需要转义,单引号不用,下面这几个是等价的
'${1}1,$3' == “\${1}1,\$3” == “奔跑吧兄弟5\${1}1,\\3”
所以为了书写简洁,便于阅读,推荐全部使用单引号。
本文发布于:2023-04-08 12:00:07,感谢您对本站的认可!
本文链接:https://www.wtabcd.cn/fanwen/zuowen/69902db6102c4f01172cd7a36550d971.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文word下载地址:PHP中 preg.doc
本文 PDF 下载地址:PHP中 preg.pdf
留言与评论(共有 0 条评论) |