如何使用正则表达式匹配PHP标签? [英] How to match PHP tags with Regex?
本文介绍了如何使用正则表达式匹配PHP标签?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
你可以给我一个匹配任何php标签的模式吗? [ <?
,<?php
]
如果你有一个很好的简单的Regex指南,我也很乐意这样做:)
我已经尝试过:
'/ ^(< \?)$ /'
但它并不真正有帮助= /
谢谢!
解决方案
您将需要preg_match_all来匹配标签,复数。以下将匹配PHP块并存储代码。
preg_match_all('/<\?php(.+? )\?> / is',$ php,$ blocks);
print_r($ blocks);
如果您的代码中没有发生字符串?>,则会出现一个问题一个PHP关闭标签。需要了解更多有关您的档案的建议。
I'm not that good at Regex,
Can you give me a pattern to match any php tags? [<?
, <?php
]
if you have a nice and simple guide for Regex i'd be happy to have it too :)
I already tried:
'/^(<\?)$/'
But it doesn't really help =/
Thanks!
解决方案
You will need preg_match_all to match tags, plural. The below will match blocks of PHP and store the code.
preg_match_all( '/<\?php(.+?)\?>/is', $php, $blocks );
print_r( $blocks );
One issue will be if you happen to have the string '?>' in your code not occuring as a PHP closing tag. Would need to know more about your file(s) to advise.
这篇关于如何使用正则表达式匹配PHP标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文