将文本拆分成单词问题PHP,复杂的问题 [英] Split text into words problem PHP, complicated problem
问题描述
我正在尝试将文本拆分为单词:
I am trying to split the text into words:
$delimiterList = array(" ", ".", "-", ",", ";", "_", ":",
"!", "?", "/", "(", ")", "[", "]", "{", "}", "<", ">", "\r", "\n",
'"');
$words = mb_split($delimiterList, $string);
在字符串上可以很好地工作,但是在某些情况下我不得不处理数字.
which works quite fine with strings but I am stuck in some cases where I have to do with numbers.
例如如果我有文字看看这个.我的分数是3.14,对此我感到高兴". 现在数组是
E.g. If I have the text "Look at this.My score is 3.14, and I am happy about it.". Now the array is
[0]=>Look,
[1]=>at,
[2]=>this,
[3]=>My,
[4]=>score,
[5]=>is,
[6]=>3,
[7]=>14,
[8]=>and, ....
然后将3.14分为3和14,在我的情况下不应该发生. 我的意思是,点应该分开两个字符串,而不是两个数字. 应该是这样的:
Then also the 3.14 is divided in 3 and 14 which should not happen in my case. I mean point should divide two strings but not two numbers. It should be like:
[0]=>Look,
[1]=>at,
[2]=>this,
[3]=>My,
[4]=>score,
[5]=>is,
[6]=>3.14,
[7]=>and, ....
但是我不知道如何避免这种情况!
But I have no Idea how to avoid this cases!
有人知道如何解决这个问题吗?
Anybody any idea how to solve this problem?
感谢, 格兰尼特(Granit)
Thanx, Granit
推荐答案
或使用正则表达式:)
<?php
$str = "Look at this.My score is 3.14, and I am happy about it.";
// alternative to handle Marko's example (updated)
// /([\s_;?!\/\(\)\[\]{}<>\r\n"]|\.$|(?<=\D)[:,.\-]|[:,.\-](?=\D))/
var_dump(preg_split('/([\s\-_,:;?!\/\(\)\[\]{}<>\r\n"]|(?<!\d)\.(?!\d))/',
$str, null, PREG_SPLIT_NO_EMPTY));
array(13) {
[0]=>
string(4) "Look"
[1]=>
string(2) "at"
[2]=>
string(4) "this"
[3]=>
string(2) "My"
[4]=>
string(5) "score"
[5]=>
string(2) "is"
[6]=>
string(4) "3.14"
[7]=>
string(3) "and"
[8]=>
string(1) "I"
[9]=>
string(2) "am"
[10]=>
string(5) "happy"
[11]=>
string(5) "about"
[12]=>
string(2) "it"
}
这篇关于将文本拆分成单词问题PHP,复杂的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!