Regular expression to get text between two lines

Hi, I need to get the contents between two lines that look like tags:

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

I am trying to use this regex, but it only works without a new line:

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

I need something that works with new characters too! and any characters that I put between these two "tags" Any ideas?

+3
source share
1 answer

Use the modifier sin your call to perform a multi-line search:

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

In the end, you should not use regex to do complex parsing. This is not up to work. Find a markup language that suits your purpose and use the existing library to analyze it.

+9
source

Source: https://habr.com/ru/post/1784822/


All Articles