如何将字符串拆分为单词和符号组合数组

时间:2021-03-09 21:36:45

I do split a sentence to words as followings:
eg.:

我把句子分成如下的单词:例如:

This is a test from php, python, asp and also from other languages. Alash! i cannot get my output as followings.  

result:

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",  
...  
)  

What can be my options in php for it?

什么可以成为我的PHP选择?

5 个解决方案

#1


2  

Try something like:

尝试以下方法:

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

#2


2  

Wow, that's a tough one! Because you want to keep "," as well. Here is what to do:

哇,这是一个艰难的!因为你想保持“,”。这是做什么的:

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

Note: in the (?<=) and in the (?=), you must put all the characters that you want to be considered as words as well, even if there is no space before and/or after them.

注意:在(?<=)和(?=)中,即使在它们之前和/或之后没有空格,也必须将您想要的所有字符都视为单词。

#3


2  

Try this method using Explode

使用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);

#4


1  

You can try somthing like

你可以试试像

$res =  preg_split( '/ |([.,])/' , $string,-1, PREG_SPLIT_DELIM_CAPTURE| PREG_SPLIT_NO_EMPTY);

#5


0  

You can use explode function with delimiter as " " (space) http://php.net/manual/en/function.explode.php

你可以使用分隔符的爆炸函数作为“”(空格)http://php.net/manual/en/function.explode.php

#1


2  

Try something like:

尝试以下方法:

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

#2


2  

Wow, that's a tough one! Because you want to keep "," as well. Here is what to do:

哇,这是一个艰难的!因为你想保持“,”。这是做什么的:

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

Note: in the (?<=) and in the (?=), you must put all the characters that you want to be considered as words as well, even if there is no space before and/or after them.

注意:在(?<=)和(?=)中,即使在它们之前和/或之后没有空格,也必须将您想要的所有字符都视为单词。

#3


2  

Try this method using Explode

使用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);

#4


1  

You can try somthing like

你可以试试像

$res =  preg_split( '/ |([.,])/' , $string,-1, PREG_SPLIT_DELIM_CAPTURE| PREG_SPLIT_NO_EMPTY);

#5


0  

You can use explode function with delimiter as " " (space) http://php.net/manual/en/function.explode.php

你可以使用分隔符的爆炸函数作为“”(空格)http://php.net/manual/en/function.explode.php