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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T01:06:35+00:00 2026-06-02T01:06:35+00:00

This is how it can be done, in several lines: // $str represents string

  • 0

This is how it can be done, in several lines:

// $str represents string that needs cleaning:
$str = " String with   line\nbreak and too  much spaces   ";
// Clean string with preg_replace():
$str = preg_replace('/[\x00-\x09\x0B-\x1F\x7F]|^ +| +$/', '', $str);
$str = preg_replace('/\x0A| +/', ' ', $str);

echo $str;
// Output:
"String with line break and too much spaces"

My quoestion focuses in combining two preg_replace() lines into one preg_replace() that does exactly same job.

Is that possible and if it is how it should be done?


There is many different uses for that behavior, one that I am after is defining regexp as constant or variable and use that within class function to clean up and validate user input.

Simplified example of suchs class:

class cleaner{
    protected $defined_methods = array(
    'TRIM' => '/ +/',
    'STRIP_CC' => '/[\x00-\x1F\x7F]/',
    'TRIM_STRIP_CC' => array('/[\x00-\x1F\x7F]/', '/ +/')
    );
    protected $defined_results = array(
    'TRIM' => ' ', 
    'STRIP_CC' => '',
    'TRIM_STRIP_CC' => array('', ' ')
    );

    function clean(array $input, array $methods){
        foreach ($input as $key => $data){
            $input[$key] = preg_replace($defined_methods[$methods[$key]], $defined_results[$methods[$key]], $data);
        }
        return $input;
    }
}

This way validation method (regexp) can vary with input data if needed so.

  • 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-02T01:06:36+00:00Added an answer on June 2, 2026 at 1:06 am
    $str = preg_replace(
        $patterns = array('/[\x00-\x09\x0B-\x1F\x7F]|^ +| +$/', '/\x0A| +/'),
        $replace  = array('',                                   ' '), 
        $str
    );
    

    See preg_replace, it supports multiple replacements after each other.

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

Sidebar

Related Questions

I understand that this can be done at release using the maven-gpg-plugin. However, I
I know that this can be easily done by using if(i%5 == 0 OR
I know this can be done and i have seen it done using some
I am wondering if this can be done easily. I am doing some paypal
Simple question. Just wondering if this can be done without me having to enforce
This can certainly be done using a simple script and reading the database. I
I'm basically work with Middleman 2 though if this can only be done in
Can this be done? It seems like this should be possible. In general, I
Can this be done in JavaScript? type == 1 ? function1() : function2();
Can this be done? Basically I work on multiple projects using xampp, and I

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.