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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T19:55:10+00:00 2026-05-15T19:55:10+00:00

I’m writing a WordPress plugin, and one of the features is removing duplicate whitespace.

  • 0

I’m writing a WordPress plugin, and one of the features is removing duplicate whitespace.

My code looks like this:

return preg_replace('/\s\s+/u', ' ', $text, -1, $count);
  • I don’t understand why I need the u
    modifier. I’ve seen other plugins
    that use preg_replace and don’t
    need to modify it for Unicode. I
    believe I have a default installation
    of WordPress .

  • Without the modifier, the code
    replaces all the spaces with Unicode
    replacement glyphs instead of spaces.

  • With the u modifier, I don’t get
    the glyphs, and it doesn’t replace all the whitespace.

Each space below has from 1-10 spaces. The regex only removes on space from each group.

Before:

This sentence  has extra space.  This doesn’t.  Extra  space, Lots          of extra space.

After:

This sentence has extra space. This doesn’t. Extra space, Lots         of extra space.

$count = 9

How can I make the regex replace the whole match with the one space?


Update: If I try this with regular php, it works fine

$new_text = preg_replace('/\s\s+/', ' ', $text, -1, $count);

It only breaks when I use it within the wordpress plugin.
I’m using this function in a filter:

function jje_test( $text ) {
    $new_text = preg_replace('/\s\s+/', ' ', $text, -1, $count);
    echo "Count: $count";
    return $new_text;
}

add_filter('the_content', 'jje_test');

I have tried:

  • Removing all other filters on the_content
    remove_all_filters('the_content');
  • Changing the priority of the filter added to the_content, earlier or later
  • All kinds of permutations of \s+, \s\s+, [ ]+ etc.
  • Even replacing all single spaces with an empty string, will not replace the spaces
  • 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-15T19:55:11+00:00Added an answer on May 15, 2026 at 7:55 pm

    This will replace all sequences of two or more spaces, tabs, and/or line breaks with a single space:

    return preg_replace('/[\p{Z}\s]{2,}/u', ' ', $text);
    

    You need the /u flag if $text holds text encoded as UTF-8. Even if there are no Unicode characters in your regex, PCRE has to interpret $text correctly.

    I added \p{Z} to the character class because PCRE only matches ASCII characters when using shorthands such as \s, even when using /u. Adding \p{Z} makes sure all Unicode whitespace is matched. There might be other spaces such as non-breaking spaces in your string.

    I’m not sure if using echo in a WordPress filter is a good idea.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
For some reason, after submitting a string like this Jack’s Spindle from a text
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have this code to decode numeric html entities to the UTF8 equivalent character.
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I would like to count the length of a string with PHP. The string
this is what i have right now Drawing an RSS feed into the php,
I've got a string that has curly quotes in it. I'd like to replace
Does anyone know how can I replace this 2 symbol below from the string

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.