开发者

Help with preg_match

开发者 https://www.devze.com 2023-01-25 09:06 出处:网络
I\'m trying to fix a problem that only cropped up when our php script was moved to 5.x from 4.x (yeah, I know).It\'s a regex that searches a flat file for a continuous block of entries and captures th

I'm trying to fix a problem that only cropped up when our php script was moved to 5.x from 4.x (yeah, I know). It's a regex that searches a flat file for a continuous block of entries and captures the block that's located between the delimiter of double line breaks (\n\n)

Here's the preg_match code - which won't find the 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";}

here is the example flat file:

### 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开发者_JS百科_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

Any insight would be ridiculously appreciated!

Keith


The . character means every character that is not a new line character. If you want to allow the new line character, you have to add the 's' option to your regexp.

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

This matches what you want. I also removed the ? to add the U option.

Edit: cdhowie's answer is better, though. I totally forgot about the 'm' option. I won't delete mine just to show you where your mistake was.
Edit2: Actually, both regexps do different job. cdhowie's matches any line beginning by $nav_page: template= while mine matches only the line if it's in one of the big block (i.e. \n\n{your big block}.


I think the issue is the double-\n in the pattern. But you should really use something like this instead:

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

精彩评论

暂无评论...
验证码 换一张
取 消