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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T12:48:58+00:00 2026-05-18T12:48:58+00:00

We just released some code to make our software a little bit more user

  • 0

We just released some code to make our software a little bit more user friendly, and it backfired. Basically, we’re attempting to replace newlines with <br /> tags. The trouble is, sometimes our users will enter code like the following:

<a
 href='http://nowhere.com'>Nowhere</a>

When we run our code, this translates to

<a <br />href='http://nowhere.com' />Nowhere</a>

which obviously doesn’t render properly.

Is there a regular expression or a PHP function to strip, or perhaps compress, the whitespace between the attributes of an HTML tag?

Clarification: This isn’t full HTML. It’s more similar to Markdown or some other language (we will eventually be moving to Markdown, but I need a quick fix). So I can’t just parse this as regular HTML. The newlines need to be converted to <br /> tags properly.

  • 1 1 Answer
  • 3 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-18T12:48:59+00:00Added an answer on May 18, 2026 at 12:48 pm

    After some searching and much trial and error, I have come up with the following solution/hack:

    /*
     * Compress all whitespace within HTML tags (including PRE at the moment)
     */
    $regexp = "/<\/?\w+((\s+(\w|\w[\w-]*\w)(\s*=\s*(?:\".*?\"|'.*?'|[^'\">\s]+))?)+\s*|\s*)\/?>/i";
    
    preg_match_all($regexp, $text, $matches);
    
    foreach($matches[0] as $match) {
      $new_html = preg_replace('/\s+/', ' ', $match);
      $text = str_replace($match, $new_html, $text);
    }
    

    After executing this code, all HTML tags in $text will be properly formatted and valid with NO newline characters.

    I know that this isn’t the best solution, but it works, and pretty soon we’ll be migrating to a true markup language (such as Markdown).

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

Sidebar

Related Questions

I just opened some old code in the current Eclipe Juno Release Candidate and
Last Thursday we just released a new version of our website. The big change
We use git to manage our code, and just create one tag when release
So I've just released my first module for nodejs . Things that I did:
jQuery Mobile 1.1.0 Final was just released and promises: True fixed toolbars: Lightweight, CSS-based
I am an android developer and I just released a new version of my
So, the BBC just released the story that ICANN is going to approve non-latin
I know it is still not quite popular, since the spec was released just
I've just seen iOS 5.1 to iOS 6.0 API Differences released by Apple. They
I just started using the new POI 3.6 http://poi.apache.org XSSF which was released in

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.