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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T11:20:45+00:00 2026-06-14T11:20:45+00:00

My current php sentence sanitizer function: function sanitize_sentence($string) { $pats = array( ‘/([.!?]\s{2}),/’, #

  • 0

My current php sentence sanitizer function:

function sanitize_sentence($string) {
    $pats = array(
    '/([.!?]\s{2}),/',      # Abc.  ,Def
    '/\.+(,)/',             # ......,
    '/(!)!+/',              # abc!!!!!!!!
    '/\s+(,)/',             # abc   , def
    '/([a-zA-Z])\1\1/');    # greeeeeeen
    $fixed = preg_replace($pats,'$1',$string);
    $fixed = preg_replace('/,(?!\s)/',', ',$fixed);
    return $fixed;
}

echo sanitize_sentence('hello!!!!!!there should be a space after the exclamation mark.right???????yes.right,');

The result should be:

hello! there should be a space after the exclamation mark. right? yes. right.

So, what’s still missing is:

  1. an extra space should be inserted after any !?,.if there is more text at the right.
  2. if the last character is a comma (or any other character different than a-zA-Z0-9!?.), it should be replaced with a dot.
  3. if the user writes more than one question mark, it should be converted to one (?????? = ?). That is working fine for me for exclamation marks, but somehow it’s not working for the others.

Any help will be highly appreciated!

  • 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-14T11:20:47+00:00Added an answer on June 14, 2026 at 11:20 am

    Your requirements:

    1. An extra space should be inserted after any !?,. if there is more text at the right.

      We can use another regex to do this replacement:

      $fixed = preg_replace( '/([!?,.])(\S)/', '$1 $2', $fixed); # spaces after punctuation, if it doesn't exist already
      
    2. If the last character is a comma (or any other character different than a-zA-Z0-9!?.), it should be replaced with a dot.

      You can get this with a regex, anchored at the end of the text:

      $fixed = preg_replace( '/[^a-zA-Z0-9!?.]+$/', '.', $fixed); # end of string must end in period
      
    3. If the user writes more than one question mark, it should be converted to one (?????? = ?). That is working fine for me for exclamation marks, but somehow it’s not working for the others.

      It’s not working because ? is a special character in regexes, you need to escape it. Replace the appropriate entry with this:

      '/(!|\?)\1+/',              # abc!!!!!!!!, abc?????????
      

    And now, the output is:

    hello! there should be a space after the exclamation mark. right? yes. right.
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

There is a php file ( current.php ) with some variables, like: function do()
Is there any way to show the current PHP function or class name in
/^(.+?) (is|was) an? (.+?)$/ is my current regular expression for PHP's preg_match function. But
Here is how our current php development solution is set up: Each developer work
I have the current PHP: $thisarray[] = #000; SetProfileField('usercss', $USER->id, implode(',',$thisarray)); Which writes #000
Possible Duplicate: get name of current PHP script in include file Can an included
Trying to display current time with PHP (using this ): $date = date('m/d/Y h:i:s
Using Php I would like to extract the current Url of a page including
I have to display current server host (in PHP, but it is not very
I'm using PHP to read the current CPU usage. I'm on a vServer, so

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.