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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T05:47:43+00:00 2026-06-12T05:47:43+00:00

Basically I’m taking in a paragraph filled with all kinds of punctuation such as

  • 0

Basically I’m taking in a paragraph filled with all kinds of punctuation
such as ! ? . ; ” and splitting them into sentences.
The issues I’m facing is coming up with a way to split them into sentences with punctuation intact while at the same time accounting for quotations in dialogue

For instance the paragraph:

One morning, when Gregor Samsa woke from troubled dreams, he found
himself transformed in his bed into a horrible vermin. “What has
happened!?” he asked himself. “I… don’t know.” said Samsa, “Maybe
this is a bad dream.” He lay on his armour-like back, and if he lifted
his head a little he could see his brown belly, slightly domed and
divided by arches into stiff sections.

Would need to be split up like this

[0] One morning, when Gregor Samsa woke from troubled dreams, he found himself transformed in his bed into a horrible vermin. 

[1] "What has happened!?" he asked himself.

[2] "I... don't know." said Samsa, "Maybe this is a bad dream."

And so on.

Currently I am just using explode

$sentences = explode(".", $sourceWork);

and only splitting it up by the periods and appending one at the end. Which I know is far from what I want but I’m not quite sure where to even start handling this. If someone could at least point me the right direction of where to look for ideas that would be amazing.

Thanks in advance!

  • 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-12T05:47:44+00:00Added an answer on June 12, 2026 at 5:47 am

    Here’s what I have:

    <?php
    
    /**
     * @param string $str                          String to split
     * @param string $end_of_sentence_characters   Characters which represent the end of the sentence. Should be a string with no spaces (".,!?")
     *
     * @return array
     */
    function split_sentences($str, $end_of_sentence_characters) {
        $inside_quotes = false;
        $buffer = "";
        $result = array();
        for ($i = 0; $i < strlen($str); $i++) {
            $buffer .= $str[$i];
            if ($str[$i] === '"') {
                $inside_quotes = !$inside_quotes;
            }
            if (!$inside_quotes) {
                if (preg_match("/[$end_of_sentence_characters]/", $str[$i])) {
                    $result[] = $buffer;
                    $buffer = "";
                }
            }
        }
        return $result;
    }
    
    $str = <<<STR
    One morning, when Gregor Samsa woke from troubled dreams, he found himself transformed in his bed into a horrible vermin. "What has happened!?" he asked himself. "I... don't know." said Samsa, "Maybe this is a bad dream." He lay on his armour-like back, and if he lifted his head a little he could see his brown belly, slightly domed and divided by arches into stiff sections.
    STR;
    
    var_dump(split_sentences($str, "."));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Basically I wanna know if all the types in a particular namespace implements a
Basically I need to take two arrays, merge them with unique values and sum
Basically, I've seen this used all to often: public event MyEventHandler MyEvent; private void
Basically, I'd like to force a variable, lets call him jim into a plugin
Basically, something better than this: <input type=file name=myfile size=50> First of all, the browse
Basically I need to get older version of a file in the repository without
Basically I want to check the extended permissions the user has granted my app.
Basically I want to convert a virtual path to an absolute path.
Basically, I have a UIImageView that will loop through 8 PNGs over 0.5 seconds.
basically I want to validate whether or not a text box is a certain

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.