我有这段文字:
男士夹克是绿色的。他——现代史上最大的明星——骑自行车的速度非常快(每小时 230 公里)。这怎么可能?!他用的是什么自行车?他的自行车的半自动齿轮非常昂贵,对达到这个速度有很大帮助。一些(或者可能很多)声称他是世界上最快的!“我看见他骑自行车了!” John Deer 先生发言。“他设定的速度是每小时133.78公里”,听起来不可思议;听起来很骗人。
我想要以下结果数组:
words[1] = "A"
words[2] = "man's"
words[3] = "jacket"
...
words[n+1] = "color"
words[n+2] = "."
words[n+3] = "He"
words[n+4] = "-"
words[n+5] = "the"
...
该数组应分别包含所有单词和标点符号。可以使用正则表达式执行吗?任何人都可以帮助编写它吗?谢谢!
编辑:根据要求展示我的作品。我正在使用以下函数处理文本,但我想在正则表达式中做同样的事情:
$text = explode(' ', $this->rawText);
$marks = Array('.', ',', ' ?', '!', ':', ';', '-', '--', '...');
for ($i = 0, $j = 0; $i < sizeof($text); $i++, $j++) {
$skip = false;
//check if the word contains punctuation mark
foreach ($marks as $value) {
$markPosition = strpos($text[$i], $value);
//if contains separate punctation mark from the word
if ($markPosition !== FALSE) {
//check position of punctation mark - if it's 0 then probably it's punctuation mark by itself like for example dash
if ($markPosition === 0) {
//add separate mark to array
$words[$j] = new Word($j, $text[$i], 2, $this->phpMorphy);
} else {
$words[$j] = new Word($j, substr($text[$i], 0, strlen($text[$i]) - 1), 0, $this->phpMorphy);
//add separate mark to array
$punctMark = substr($text[$i], -1);
$j += 1;
$words[$j] = new Word($j, $punctMark, 1, $this->phpMorphy);
}
$skip = true;
break;
}
}
if (!$skip) {
$words[$j] = new Word($j, $text[$i], 0, $this->phpMorphy);
}
}