PHP exploding a string while keeping delimiters

2019-08-26 22:12发布

问题:

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.

回答1:

$array = preg_split('~([.!?:;])~u',$raw , null, PREG_SPLIT_DELIM_CAPTURE);


回答2:

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? Sex On Hard Concrete Always Hurts The Orgasmic Area. Why does custard taste so lumpy when you use breast milk?";

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