How to match PHP tags with Regex? [closed]

我的梦境 提交于 2019-12-24 13:39:27

问题


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!


回答1:


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.



来源:https://stackoverflow.com/questions/20315444/how-to-match-php-tags-with-regex

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!