3

Hi i need to get the content between 2 strings similar to tags like this:

[code]
some text and 
new line 
[/code]

I try with this regular expression but it works only without new line :

preg_match("/\[view\](.*)\[\/view\]/",$string, $results);

I need something that works also with newlines! and any characters i put between these 2 "tags" Any idea ?

1 Answer 1

9

Use the s modifier in your call to do a multiline search:

preg_match("/\[view\](.*)\[\/view\]/s",$string, $results);

In the end, you should not be using regex to do complex parsing. It isn't up to the job. Find a markup language that suits your purpose and use an existing library to parse it.

Sign up to request clarification or add additional context in comments.

4 Comments

Thank you! it works also with a "s" modifier (just discovered:P) Thank you
@Asap In fact it does not work with an m modifier; the correct answer is, as you say, the s modifier. Sorry for my previous incorrect answer...
Try running it against [view]foo[view]bar[/view]baz[/view], what will you get?
@ircmaxwell It's a good point, and I've added a note about using a real parser.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.