Jump to content
WorldDrknss

Matching between comment lines

Recommended Posts

I'm trying to match

<!-- FOR 5 -->

Some HTML content 

<!-- END FOR -->

Using preg_match

\<\!\-\- FOR (\d+) \-\-\>(.*)\<\!\-\- END FOR \-\-\>

Doesn't work since they are on different lines. 

 

Share this post


Link to post
Share on other sites

First you need to learn that < ! - > are not special characters. Escaping them with backslashes makes you look a bit silly.

Then learn about the /x and /s flags. One of them is what you need. The other is me trying to trick you into learning something unrelated.

Then test your regular expression with some HTML content that contains two or more of those FOR/END FORs and see what happens.

Share this post


Link to post
Share on other sites

Also, you need to look into how to make your capturing conditions "greedy" or "non greedy". By default, matches will be greedy. So a condition such as "A(.*)B" with the string "A1B A2B A3B" would find one match "1B A2B A3" - everything form the first "A" to the last "B". If you wanted to find all the values between each set of A/B, then you need make the match non-greedy - "A(.*?)B"

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.