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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T13:37:24+00:00 2026-06-06T13:37:24+00:00

I am trying to fix some HTML, and it is working perfect. But one

  • 0

I am trying to fix some HTML, and it is working perfect. But one thing is givind me troubles; specifically the open and close <p></p> tags.
I need to fix two cases:
a) Remove the <p> </p> sequences.
b) Remove end and start blankspaces IN paragraphs.
I am doing this:

preg_replace("#<p>\s*</p>#","",$str);
preg_replace("#\s?(</?p>)\s?#", '$1', $str);

But none of both is replacing anything. What am I doing wrong, not tottaly right?

Edit: original code

$source=preg_replace("#\s?(</?p>)\s?#", '$1', $source); //Replace possible innecessary blank spaces
return preg_replace("#<p>\s*</p>#","",$source); //Delete all posible empty pharagraphs

Thank you 😀

  • 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-06-06T13:37:26+00:00Added an answer on June 6, 2026 at 1:37 pm
    $source = preg_replace('#<p>\s*</p>|(?<=<p>)\s+|\s+(?=</p>)#', '', $source);
    

    Use single quotes for clearer back-slashes. Did you mean \s or \\s? Luckily, in this case it doesn’t matter.

    The above PCRE will remove <p> </p> tags that only have whitespace (or nothing) between them, whitespace just after <p> and whitespace just before </p>. I don’t see why your original code wouldn’t have worked, though. You should have removed a single space both before or after any <p> or </p> tag (i.e. " </p> " would become "</p>".

    If you’re still having trouble you need to post more code. Are you passing your variable in correctly, etc.? Try to write a minimal case with your problem and post the whole program.

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

Sidebar

Related Questions

I'm trying to fix something in some Objective C++ (?!) code. I don't know
I really need some help with this as I have been trying to fix
Im trying to add some short file names to a Wise installer to fix
I'm trying to fix single quote bug in the code: std::string Index; connection->Open(); String^
trying to fix a CSS bug for some code that someone else wrote. I'm
I'm trying to add some jQuery + ERB to a specific view: views/posts/show.html.erb (top
I'm working on an application which retrieves some HTML code from a record in
I load some html using AJAX, this works fine, one of the bits that
I was trying to do some hover effects like on nvidia page ( http://www.nvidia.com/object/cool_stuff.html
I'm trying to programmatically insert a dijit.form.Button into some HTML and my code goes

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.