Fueling Your Coding Mojo

Buckle up, fellow PHP enthusiast! We're loading up the rocket fuel for your coding adventures...

Popular Searches:

Regular expression for randomly appear new lines (PHP)

User: Hi, I'm facing an issue with handling new lines in my PHP code. I have a string where new lines randomly appear and I need to extract specific patterns from it using regular expressions. However, the position of these new lines is not fixed, so I'm struggling with creating the correct regular expression.

For example, let's say I have the following string:

"Lorem ipsum dolor sit amet,
consectetur adipiscing elit,
sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."

In this case, new lines are randomly inserted within the string, which makes it difficult for me to capture the desired patterns.

I want to extract specific patterns, such as all words that start with a lowercase letter and end with "et". The regular expression I have so far is:


However, this expression fails to capture the desired patterns when new lines are present in the string.

Can someone please help me modify this regular expression to handle the scenario where new lines randomly appear? Any assistance or alternative approaches would be greatly appreciated!

All Replies


User 1: Hey there! I've encountered a similar issue with handling new lines in PHP. Regular expressions can indeed be a bit tricky when it comes to dealing with random line breaks. In cases like this, the "m" modifier, also known as the multi-line mode, can be quite handy.

To modify your existing regular expression, you can simply add the "m" modifier at the end of it. So your expression would now look like:


This "m" modifier helps your regular expression treat the string as multiple lines rather than a single line, allowing it to look for patterns across newline characters.

In your example, this modified regular expression should capture all the words starting with a lowercase letter and ending with "et", regardless of line breaks within the string.

Give it a try and let me know if it helps! If you have any other questions or need further clarification, feel free to ask.


User 2: Hi everyone, I've also had to deal with handling new lines in PHP when working with regular expressions. It can be a little tricky, but I found an alternative approach that might help you solve this issue.

Instead of relying solely on regular expressions, you could use the `preg_split()` function to split your string into an array based on new lines. This way, you can process each line individually and apply regular expressions to each separate line.

Here's an example code snippet to illustrate this approach:

$string = "Lorem ipsum dolor sit amet, \n
consectetur adipiscing elit, \n
sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.";

$lines = preg_split("/\n/", $string);

foreach ($lines as $line) {
// Apply your regular expression to each $line here
// and capture the desired patterns

By splitting the string into separate lines using `preg_split()` and then iterating through each line, you have more flexibility in applying regular expressions without being affected by random line breaks.

Give it a try and see if it works for your specific use case. Feel free to reach out if you have any further questions or need more assistance.

New to LearnPHP.org Community?

Join the community