Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6049641
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T07:35:58+00:00 2026-05-23T07:35:58+00:00

I agree with the PHP Modifiers Reference in the ‘m’ modifier description that says:

  • 0

I agree with the PHP Modifiers Reference in the ‘m’ modifier description that says:

When this modifier is set, the “start of line” and “end of line” constructs match immediately following or immediately before any newline in the subject string, respectively, as well as at the very start and end.

I’m learning regular expressions and this example is a little confusing:

<?php echo preg_replace("/^[\s]*$/m","<p>","with.\n\n\n\t \nTherefor"); ?>

It ends up with:

“with.\n

\nTherefor”

But I put mentally the ‘^’ and ‘$’ metacharacters like previously said.
(^,$=imaginal positions)

^with.$\n^$\n^$\n^\t $\n^Therefor$

And reading the regex:

A start of line followed or not by one space character ([ \t\r\n]) followed by the end of the line.

But the regex engine takes from the second ‘^’ to the fourth ‘$’, eating up the ‘\n’. Shouldn’t it be like this?:

(^,$=imaginal positions)

^with.$\n <br/>
^(no space character)$\n <br/>
^(no space character)$\n <br/>
^(\t )$\n <br/>
^Therefor$


with.\n <br/>
<p>\n <br/>
<p>\n <br/>
<p>\n <br/>
Therefor

It is supposed that the regex runs toward the right.
Why does the regex seem lookahed the ‘\n’?

Because it matches at the point ^(no space character)$ and then follows…’ \n ‘.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T07:35:59+00:00Added an answer on May 23, 2026 at 7:35 am

    This regex seems to combine multiple lines…

    But you can avoid this, by using the U modifier xor adding a ? to *, so that it’s not longer greedy:

    echo preg_replace("/^[\s]*$/mU","<p>","with.\n\n\n\t \nTherefor");
    echo preg_replace("/^[\s]*?$/m","<p>","with.\n\n\n\t \nTherefor");
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

It seems I've got to agree with this post when it states that [...]
I agree, that programming against interfaces is a good practice. In most cases in
Most mathematicians agree that: e πi + 1 = 0 However, most floating point
Most people would agree that internationalizing an existing app is more expensive than developing
I think most C++ programmers here would agree that polluting the global namespace is
I think most of us agree that it's a good idea to use a
I think everyone here would agree that in order to be considered a professional
I'm curious about OpenID. While I agree that the idea of unified credentials is
Windbg fans claim that it is quite powerful and I tend to agree. But
We all agree that using different exception types for different tasks is the way

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.