0

For my project I needed to analyze different sentences and work out which ones were questions by determining if they ended in question marks or not.

So I tried using explode but it didn't support multiple delimiters. I temporarily replaced all punctuation to be chr(1) so that I could explode all sentences no matter what they ended with (., !, ?, etc...).

Then I needed to find the last letter of each sentence however the explode function had removed all of the punctuation, so I needed some way of putting it back in there.

It took me a long time to solve the problem but eventually I cracked it. I am posting my solution here so that others may use it.

2 Answers 2

10
$array = preg_split('~([.!?:;])~u',$raw , null, PREG_SPLIT_DELIM_CAPTURE);
Sign up to request clarification or add additional context in comments.

Comments

7

Here is my function, multipleExplodeKeepDelimiters. And an example of how it can be used, by exploding a string into different sentences and seeing if the last character is a question mark:

function multipleExplodeKeepDelimiters($delimiters, $string) {
    $initialArray = explode(chr(1), str_replace($delimiters, chr(1), $string));
    $finalArray = array();
    foreach($initialArray as $item) {
        if(strlen($item) > 0) array_push($finalArray, $item . $string[strpos($string, $item) + strlen($item)]);
    }
    return $finalArray;
}

$punctuation = array(".", ";", ":", "?", "!");
$string = "I am not a question. How was your day? Thank you, very nice. Why are you asking?";

$sentences = multipleExplodeKeepDelimiters($punctuation, $string);
foreach($sentences as $question) {
    if($question[strlen($question)-1] == "?") {
        print("'" . $question . "' is a question<br />");
    }
}

2 Comments

I must say, that has to be the strangest example string I've ever seen.
Best way to remember SOH CAH TOA that I've ever seen!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.