Home  >  Article  >  Backend Development  >  PHP核心技术与最佳实践之正则表达式反向摘引

PHP核心技术与最佳实践之正则表达式反向摘引

WBOY
WBOYOriginal
2016-06-13 12:18:02724browse

PHP核心技术与最佳实践之正则表达式反向引用

PHP核心技术与最佳实践之正则表达式反向引用

反向引用用于重复搜索前面某个分组匹配的文本。例如:“\1”代表分组1匹配的文本:

\b(\w+)\b\s+\1\b

要反向引用分组捕获的内容,可以使用“\k”上个例子:

\b(?\w+)\b\s+\k\b

【示例】:UBB标签代码

UBB标签用来在论坛和留言板中代替HTML,实现一些简单的HTML效果,同时防止滥用HTML出现安全问题。

最终UBB标签还是会要解析成HTML代码,才能让浏览器认识,以url标签为例。

“[url]1.gif[/url]”

<?PHP $str = ‘[url]1.gif[/url][url]2.gif[/url] [url]3.gif[/url]’;       $s = preg_replace(“#\[url\](?<word>\d\.gif)\[\/url\]#”,”<img src www.example alt="PHP核心技术与最佳实践之正则表达式反向摘引" >;       Var_dump($s);?></imgsrc>


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn