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

  • Home
  • SEARCH
  • 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

Ask A Question

Stats

  • Questions 165k
  • Answers 165k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Categories extend the original class, but they don't subclass it,… May 12, 2026 at 12:54 pm
  • Editorial Team
    Editorial Team added an answer Haven't tested this, but it's something like: RewriteRule \.php$ -… May 12, 2026 at 12:54 pm
  • Editorial Team
    Editorial Team added an answer "0:0:0:0:0:0:0:1" is the IPv6 loopback address as defined in RFC… May 12, 2026 at 12:54 pm

Related Questions

I'm in the process of porting a large C++ application from Linux (gcc) to
I am using FCKEditor within a Django app served by Apache/mod-wsgi. I don't want
I consider fluent interfaces very convenient for many tasks. But I feel uneasy when
We Want to Run Our C# Code on the JVM My company has a
I have set error reporting in my development environment to E_STRICT by adding to

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.