Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regular expression to get text between 2 string

Tags:

regex

php

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 ?

like image 715
MatterGoal Avatar asked Dec 01 '25 08:12

MatterGoal


1 Answers

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.

like image 94
lonesomeday Avatar answered Dec 03 '25 20:12

lonesomeday



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!