我正在使用preg_match_all,尝试匹配:
[<? or <?php]
[any amount of space here, at least one, may be newline]
[legendcool]
[any amount of space]
[(] return whatever is in here [)]
[any amount of space]
[?>]到目前为止,我有这样的想法:
index.php
$the_prophecy = file_get_contents("secret.php");
preg_match_all('~[<?|<?php]\s*[legendcool(](.*?)[)]\s*[?>]~',$the_prophecy,$matches) ;secret.php
<title>Regex Match all characters between two strings - Stack Overflow</title>
<link rel="shortcut icon" href="http://cdn.sstatic.net/stackoverflow/img/favicon.ico">
<?php legendcool({'',''}) ?>
<link rel="apple-touch-icon image_src" href="http://cdn.sstatic.net/stackoverflow/img/例如,在secret.php中,我希望获得{'',''}
你们中有谁知道如何调整我的preg_match_all,使其以我想要的方式工作?
发布于 2013-05-29 04:02:53
您的正则表达式中有几个错误:
方括号应替换为圆形ones
?应进行转义,因为它在regular expressions
s 更好的正则表达式可以是这样的:
~<\?(?:php)?(.+?)\?>~s使用上述建议,您的最终解决方案将是:
preg_match_all('~<\?(?:php)?\s+legendcool\(([^)]+)\).*?\?>~s', $the_prophecy, $matches);
print_r($matches[1]);
// OUTPUT: {'',''}发布于 2013-05-29 04:03:18
请允许我首先介绍PHP PCRE Cheat Sheet,它是您在PHP中的所有正则表达式需求的快速参考。
接下来,在正则表达式中,[和]用于字符组,基本上意味着“匹配这些字符中的任何一个”,例如,[afd]将匹配a、f或d中的任何字符。
发布于 2013-05-29 04:00:52
您混淆了方括号和括号,[<?|<?php]应为(<?|<?php)。如果你不想让它捕获任何东西,那就写(?:<?|<?php)。
https://stackoverflow.com/questions/16800378
复制相似问题