繁体   English   中英

帮助进行preg_match

[英]Help with preg_match

我正在尝试解决仅在我们的PHP脚本从4.x移至5.x时出现的问题(是的,我知道)。 这是一个正则表达式,它在平面文件中搜索连续的条目块并捕获位于双换行符(\\ n \\ n)分隔符之间的块

这是preg_match代码-找不到匹配项:

$nav_page = 'business_notebook_plan_section1';

if (preg_match("/\n\n(.*?$nav_page: template=.*?)\n\n/", $pages_blocks, $matches)) {
// then find the block that contains this page
print "found a match!";
}

else {print "preg_match failed";}

这是示例平面文件:

### BUSINESS ###
business_instructions: template=misc/instructions.html

business_notebook_intro: template=notebook/notebook.html&column2=1
business_notebook_plan_section1: template=notebook/notebook.html&column2=1
business_notebook_format_section1: template=notebook/notebook.html&column2=1
business_notebook_steps: template=notebook/notebook.html&column2=1&activity=poll
business_notebook_step1: template=notebook/notebook.html&column2=1
business_notebook_step2: template=notebook/notebook.html
business_notebook_step3: template=notebook/notebook.html&column2=1&activity=poll_text
business_notebook_step4: template=notebook/notebook.html&column2=1&activity=poll_text
business_notebook_step5_section1: template=notebook/notebook.html&column2=1
business_notebook_step6: template=notebook/notebook.html
business_notebook_step7: template=notebook/notebook.html&column2=1
business_notebook_review: template=notebook/notebook.html&num_questions=3
business_notebook_review_feedback: template=notebook/notebook.html&num_questions=3
business_notebook_summary: template=notebook/notebook.html&column2=1

business_notebook_intro_popup: template=shared/transcript_popup.html
business_notebook_intro_video: template=shared/video_popup.html
business_notebook_plan_section2: template=notebook/notebook.html&column2=1
business_notebook_steps_results: template=notebook/notebook.html&column2=1&activity=poll
business_notebook_format_section2: template=notebook/notebook.html&column2=1
business_notebook_step5_section2: template=notebook/notebook.html&column2=1
business_notebook_step5_section3: template=notebook/notebook.html&column2=1
business_notebook_step7_popup: template=shared/transcript_popup.html
business_notebook_step7_video: template=shared/video_popup.html
business_notebook_summary_popup: template=shared/transcript_popup.html
business_notebook_summary_video: template=shared/video_popup.html

business_resources: template=resources/resources.html&cells=2

business_preparation: template=preparation/preparation.html&cells=0

business_preparation_popup: template=preparation/preparation_popup.html&cells=0

任何见识将被荒谬地赞赏!

基思

的。 字符表示不是换行符的每个字符。 如果要允许换行符,则必须在正则表达式中添加“ s”选项。

preg_match("/\n\n(.*$nav_page: template=.*)\n\n/sU", $pages_blocks, $matches)

这符合您的要求。 我也删除了? 添加U选项。

编辑:cdhowie的答案更好。 我完全忘记了“ m”选项。 我不会删除我的内容只是为了向您显示您的错误所在。
Edit2:实际上,两个正则表达式都做不同的工作。 cdhowie匹配以$nav_page: template=开头的任何行,而我的仅匹配位于大块之一(即\\ n \\ n {your big block})中的行。

我认为问题在于模式中的双\\n 但是,您实际上应该使用如下所示的内容:

preg_match("/^($nav_page: template=.*?)$/m", $pages_blocks, $matches)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM