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

The Archive Base Latest Questions

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

This is a sentence sanitizer. function sanitize_sentence($string) { $pats = array( ‘/([.!?]\s{2}),/’, # Abc.

  • 0

This is a sentence sanitizer.

function sanitize_sentence($string) {
    $pats = array(
    '/([.!?]\s{2}),/',      # Abc.  ,Def
    '/\.+(,)/',             # ......,
    '/(!|\?)\1+/',          # abc!!!!!!!!, abc?????????
    '/\s+(,)/',             # abc   , def
    '/([a-zA-Z])\1\1/');    # greeeeeeen
    $fixed = preg_replace($pats,'$1',$string); # apply pats
    $fixed = preg_replace('/(?:(?<=\s)|^)[^a-z0-9]+(?:(?=\s)|$)/i', '',$fixed); # bad chunks
    $fixed = preg_replace( '/([!?,.])(\S)/', '$1 $2', $fixed); # spaces after punctuation, if it doesn't exist already
    $fixed = preg_replace( '/[^a-zA-Z0-9!?.]+$/', '.', $fixed); # end of string must end in period
    $fixed = preg_replace('/,(?!\s)/',', ',$fixed); # spaces after commas
    return $fixed;
}

This is the test sentence:

hello [[[[[[]]]]]] friend…..? how are you [}}}}}}

It should return:

hello friend…..? how are you

But instead it is returning:

hello friend. .. .. ? how are you.

So there are 2 problems and I can’t find a solution around them:

  1. the set of periods are being separated into ".. .. ." for some reason. They should remain as "….." next to the question mark.
  2. the end of the string must end in a period only and only if there is at least one of these characters anywhere in the string: !?,. (if at least one of those characters are not found in the string, that preg_replace should not be executed)

Examples for the second problem:

This sentence doesn’t need an ending period because the mentioned characters are nowhere to be found

This other sentence, needs it! Why? Because it contains at least one of the mentioned characters

(of course, the ending period should only be placed if it doesn’t exist yet)

Thanks for your help!

  • 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-14T12:16:46+00:00Added an answer on June 14, 2026 at 12:16 pm

    Here is the answer to your first problem. The third-to-last replacement is the problem:

    $fixed = preg_replace( '/([!?,.])(\S)/', '$1 $2', $fixed); # spaces after punctuation, if it doesn't exist already
    

    It will match the first period with the character class, and the second period as a non-space character. Then insert a space. Since matches cannot overlap, it will then match the third and forth period and insert a space and so on. This is probably best fixed like this:

    $fixed = preg_replace( '/[!?,.](?![!?,.\s])/', '$0 ', $fixed);
    

    Here is how you could go about your second requirement (replace the second-to-last preg_replace):

    $fixed = trim($fixed);
    $fixed = preg_replace( '/[!?.,].*(?<![.!?])$/', '$0.', $fixed);
    

    First we get rid of leading and trailing whitespace to separate this concern from the trailing period. Then the preg_replace will try to find a punctuation character in the string and if it does, it matches everything until the end of the string. The replacement puts the match back in place and appends the period. Note the negative lookbehind. It asserts that the string does not already end with a sentence-ending punctuation character.

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

Sidebar

Related Questions

I have a varargs constructor like this : public class Sentence { public String[]
I have a string This is a big sentence . ! ? ! but
Why this sentence is valid in C++? qi::rule<Iterator, std::string(), skipper<Iterator> > name; Extracted from
In this quicksort function: def qsort2(list): if list == []: return [] else: pivot
This sentence b.followees_by_type(biscuit) return something like: => [#<Biscuit _id: 4fdf5aa11d41c829ea000020, _type: Biscuit, created_at: 2012-06-18
Again, I saw this sentence: A .classpath file snippet that can be included in
C# It's possible to set to a variant this sentence {D , and receive
I am reading the Java Hashmap documentation but I don't understand this sentence. Note
I'm using MySQL 5, and I need to do this sentence to get the
I have a sentence like this. 1 &nbsp; &nbsp; &nbsp; 2 &nbsp; &nbsp; 3

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.