I need to do multiple regexes in a large text and sometimes they're multiple. My problem is; when I add a regex it works perfectly fine in a short but when it's on a longer one it doesn't find the regex. When add /s to the end to activate "dot matches newline
", it just breaks the regex because it finds one on from the start and one from the end.
preg_match_all('/\[condition](.*?)\[\/condition]*\]/', $content, $result);
Example text of not being able to find regex without /s.
[condition]Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla vel lobortis nisl. Proin ut magna nec eros euismod posuere. Aliquam posuere ornare hendrerit. Praesent non ultrices metus. [somecondition]Duis pretium posuere faucibus.[/somecondition] Etiam feugiat porta diam lacinia pretium. Praesent vestibulum dapibus faucibus. Donec aliquet purus ut erat fermentum, quis tincidunt massa hendrerit.Donec vitae neque gravida, viverra elit vel, lacinia justo. Vivamus sed lacus velit. Curabitur commodo sollicitudin pretium. Sed lobortis urna ac [anothercondition] urna aliquam, eu viverra felis tincidunt. Quisque sagittis consectetur felis sit amet venenatis.[/anothercondition] Donec vel convallis justo, a molestie magna. Phasellus ultrices augue molestie, fringilla quam vel, ornare lorem. Vestibulum nec arcu in orci hendrerit tristique.[/condition]
preg_match_all('/\[condition](.*?)\[\/condition]*\]/s', $content, $result);
Example text of not being able to find regex with /s.
[condition]Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla vel lobortis nisl.[anothercondition]Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla vel lobortis nisl. [/anothercondition] [/condition]
[condition]Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla vel lobortis nisl.[anothercondition]Proin ut magna nec eros euismod posuere. Aliquam posuere ornare hendrerit. Praesent non ultrices metus. Duis pretium posuere faucibus.[/anothercondition] [/condition]
[condition]Proin ut magna nec eros euismod posuere. Aliquam posuere ornare hendrerit. Praesent non ultrices metus. Duis pretium posuere faucibus.[anothercondition]Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla vel lobortis nisl. [/anothercondition] [/condition]
I'd really appreciate if you can tell me what I'm missing. Thanks
My Answer:
multiline mode, it still doesn't work. What am I doing wrong?
Here is an example of my code:
php
$text = "This is a sample text
with multiple lines
and some regex patterns";
// First regex pattern
$pattern1 = "/sample text/s";
preg_match($pattern1, $text, $matches1);
print_r($matches1);
// Second regex pattern
$pattern2 = "/multiple lines/s";
preg_match($pattern2, $text, $matches2);
print_r($matches2);
In this code, the first regex pattern works fine and finds the match in the text. However, the second regex pattern doesn't work and doesn't find the match.
To fix this issue, you can use the `s` modifier at the end of each regex pattern to enable the dot `.` to match newline characters as well. Here is the updated code:
php
$text = "This is a sample text
with multiple lines
and some regex patterns";
// First regex pattern
$pattern1 = "/sample text/s";
preg_match($pattern1, $text, $matches1);
print_r($matches1);
// Second regex pattern
$pattern2 = "/multiple lines/s";
preg_match($pattern2, $text, $matches2);
print_r($matches2);
By adding the `s` modifier at the end of each regex pattern, the dot `.` will now match newline characters as well, allowing the second regex pattern to find the match in the text.
Rate this post
5 of 5 based on 8930 votesComments