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 335077
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T10:06:55+00:00 2026-05-12T10:06:55+00:00

I am modifying a core function of the Kohana library, the text::auto_p() function. The

  • 0

I am modifying a core function of the Kohana library, the text::auto_p() function.

The function describes itself as “nl2br() on steroids”. Essentially, it provides <br /> single line breaks, but double line breaks are surrounded with the <p> tags.

The limitation I have found with it is that it will but <br />s in a <pre> element. This will create double new lines, which isn’t what I want. I have made a modification to pick up pre elements with a regex, and a callback that will strip out the <br /> which works alright.

However, the main problem is that I have code samples in my text that gets auto_p()‘d, and I need to preserve the indentation (for readability). Unfortunately for me, the function strips leading and trailing white space on lines.

Here is the regex that strips leading space

$str = preg_replace('~^[ \t]+~m', '', $str);

I’m not the best regex guru, but I’m pretty sure that says “Get leading spaces and tabs where there is at least one and replace them with an empty string.”

I have tried removing this line, but then it will add <br /> where I definitely do not want them – in one case, I was getting output like this

<ul><br />
    <li>something</li>
</ul>

How would I modify this regex or code to not strip leading space inside of a <pre> element?

The original helper function from Kohana is available here. (scroll to the almost bottom).

I know I will get a few ‘Use a HTML parser’ type answers – and while you may be correct – the existing code simply uses regex, and I would prefer a simpler solution (where I don’t have to include a library etc).

Thanks for your time.

  • 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-12T10:06:55+00:00Added an answer on May 12, 2026 at 10:06 am

    Here’s how I would do it:

    $str = preg_replace(
        '~^[ \t]++(?=(?:[^<]++|<(?!/?+pre\b))*+(?:\z|<pre\b))~im',
        '', $str);
    

    After matching some line-leading whitespace, the lookahead scans ahead for <pre> or </pre> tags. The meat of the lookahead is this bit:

    (?:[^<]++|<(?!/?+pre\b))*+
    

    It matches zero or more of anything that’s not a left angle bracket, or a left angle bracket if it’s not the beginning of a <pre> or </pre> tag. That part will only stop matching when it encounters a <pre> (starting) tag, a </pre> (ending) tag, or the end of the input. If it’s an ending tag that stops it, you know you’re inside a <PRE> element, so you don’t want to do the replacement.

    The possessive quantifiers ('++', '*+', and '?+') are essential to prevent catastrophic backtracking. (I can’t help it: that phrase always makes me think of the resonance cascade scenario from Half-Life.)

    This technique also assumes reasonably well-formed HTML, i.e., all <pre>...</pre> tags properly balanced. Tags inside of SGML comments will mess it up, too–unless they happen to be balanced. You can deal with comments, too, if you don’t mind making the regex twice as long and three times as ugly. 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working on modifying Firaxis' Civilization 4 core game DLL. The host application is
I'm trying to modify the 'attr' function in the jQuery (V.6.1) core. I have
Say if core A is modifying a variable X and core B is reading
What are some non prototype modifying libraries that provide core functionality to JavaScript (not
Without modifying the form itself, can we make the from only show for some
How does one extend core JavaScript types (String, Date, etc.) without modifying their prototypes?
I'm doing some contribution to an open source library, but I'm having trouble modifying
Without modifying and recompiling the gnu gcc and stdc++ library builds, I need to
How can I add my custom driver without modifying DriverManager.php in the Doctrine2 core?
Modifying the HTTP Response Using Filters

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.