MatterGoal
MatterGoal

Reputation: 16430

Regular expression to get text between 2 string

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 ?

Upvotes: 3

Views: 5472

Answers (1)

lonesomeday
lonesomeday

Reputation: 237837

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.

Upvotes: 9

Related Questions