PHP exploding a string while keeping delimiters

时光毁灭记忆、已成空白 提交于 2019-12-12 05:10:13

问题


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


来源:https://stackoverflow.com/questions/16749178/php-exploding-a-string-while-keeping-delimiters

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!