Search code examples
phpstringexplodedelimiter

PHP exploding a string while keeping delimiters


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.


Solution

  • 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 />");
        }
    }