我该如何将字符串分割成单词和符号组合的数组?

5
我会将句子分成以下单词:
例如:
This is a test from php, python, asp and also from other languages. Alash! i cannot get my output as followings.  

结果:

结果:

array(  
[0]=>"This",  
[1]=>"is",  
[2]=>"a",  
[3]=>"test",  
[4]=>"from",  
[5]=>"php",  
[6]=>",",  
[7]=>"python",  
[8]=>",",  
[9]=>"asp",  
[10]=>"and",  
[11]=>"also",  
[12]=>"from",  
[13]=>"other",  
[14]=>"languages",  
[15]=>".",  
[16]=>"Alash",  
[17]=>"!",  
[18]=>"I",  
[19]=>"cannot",  
[20]=>"get",  
...  
)  

在php中,我的选项有哪些?

5个回答

2

哇,这个有点难!因为你想保留逗号“,”。以下是解决方法:

$string = "I beg to differ, you can get it as the previous.";
$words = preg_split('/\s+|(?<=[,\.!\?])|(?=[,\.!\?])/',$string);

注意:在 (?<=)(?=) 中,您必须将所有想要被视为单词的字符都放在里面,即使它们之前和/或之后没有空格。请保留HTML标签,不做解释。

2
尝试使用 Explode 方法:
function multiexplode ($delimiters,$string) 
{

    $ready = str_replace($delimiters, $delimiters[0], $string);
    $launch = explode($delimiters[0], $ready);
    return  $launch;
}

$text = "here is a sample: this text, and this will be exploded. this also | this one too :)";

$exploded = multiexplode(array(",",".","|",":"),$text);

print_r($exploded);

2
尝试类似以下的内容:

请尝试:

preg_split('/\s+|\b/', $string)

那真是太棒了...如果能省略空格就更好了。 - KoolKabin
1
最好使用 /\s+|\b(?=\W)/,这样就不会得到那么多空字符串了。 - mhyfritz
你在这里得到了空格?当我尝试在Perl中执行split时,我并没有得到它。perl -e "print join'#', split /\s+|\b/, 'foo, bar baz! k'"给出的结果是 foo#,#bar#baz#!#k,没有任何空格。我猜PHP的split函数工作方式不同。 - Qtax

1
你可以尝试类似这样的代码:
$res =  preg_split( '/ |([.,])/' , $string,-1, PREG_SPLIT_DELIM_CAPTURE| PREG_SPLIT_NO_EMPTY);

0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接