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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T06:06:46+00:00 2026-06-06T06:06:46+00:00

I have a $text to strip off all non-alphanumeric chars, replace multiple white spaces

  • 0

I have a $text to strip off all non-alphanumeric chars, replace multiple white spaces and newline by single space and eliminate beginning and ending space.

This is my solution so far.

$text = '
some-    text!! 

for testing?
'; // $text to format

//strip off all non-alphanumeric chars
$text = preg_replace("/[^a-zA-Z0-9\s]/", "", $text);

//Replace multiple white spaces by single space 
$text = preg_replace('/\s+/', ' ', $text);

//eliminate beginning and ending space
$finalText = trim($text);
/* result: $finalText ="some text for testing";
without non-alphanumeric chars, newline, extra spaces and trim()med */

Is it possible to combine/achieve all these in one regular expression? as I would get the desired result in one line as below

$finalText = preg_replace(some_reg_expression, $replaceby, $text);

thanks

Edit: clarified with a test string

  • 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-06T06:06:48+00:00Added an answer on June 6, 2026 at 6:06 am

    Of course you can. That is very easy.

    The re will look like:

    ((?<= )\s*)|[^a-zA-Z0-9\s]|(\s*$)|(^\s*)
    

    I have no PHP at hand, I have used Perl (just to test the re and show that it works) (you can play with my code here):

    $ cat test.txt 
             a       b       c    d
    a b c e f g             fff  f
    
    $ cat 1.pl 
    while(<>) {
        s/((?<= )\s*)|[^a-zA-Z0-9\s]|(\s*$)|(^\s*)//g;
        print $_,"\n";
    }
    
    $ cat test.txt | perl 1.pl 
    a b c d
    a b c e f g fff f
    

    For PHP it will be the same.

    What does the RE?

    ((?<= )\s*)       # all spaces that have at least one space before them
    |
    [^a-zA-Z0-9\s]    # all non-alphanumeric characters
    |
    (\s*$)            # all spaces at the end of string
    |
    (^\s*)            # all spaces at the beginning of string
    

    The only tricky part here is ((?<= )\s*), lookbehind assertion. You remove spaces if and only if the substring of spaces has a space before.

    When you want to know how lookahead/lookbehind assertions work, please take a look at http://www.regular-expressions.info/lookaround.html.

    Update from the discussion:

    What happens when $text ='some ? ! ? text';?
    Then the resulting string contains multiple spaces between “some” and “text”.

    It is not so easy to solve the problem, because one need positive lookbehind assertions with variable length, and that is not possible at the moment. One cannot simple check spaces because it can happen so that it is not a space but non-alphanumerich character and it will be removed anyway (for example: in " !" the "!" sign will be removed but RE knows nothing about; one need something like (?<=[^a-zA-Z0-9\s]* )\s* but that unfortunately will not work because PCRE does not support lookbehind variable length assertions.

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

Sidebar

Related Questions

how do I strip off all white space and &nbsp; ? I have this
I have a text file with two non-ascii bytes (0xFF and 0xFE): ??58832520.3,ABC 348384,DEF
I am trying to strip and replace a text string that looks as follows
I have a windows form with a tool strip on it containing a text
Previously I have asked to strip text from a field and convert it to
I have the following text that I want to strip out and grab the
I have text like this format term: 156^^^:^^59 datainput Or term: 156^^^:59 datainput or
I have text within a paragraph tag that that fits snug on the bottom
I have text input boxes. There is validation for each of the boxes using
I have text I am displaying in SIlverlight that is coming from a CMS

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.