php – 如何将字符串拆分为单词对?
发布时间:2020-12-13 22:07:50  所属栏目:PHP教程  来源:网络整理 
            导读:我试图在 PHP中将字符串拆分为单词对数组.例如,如果您有输入字符串: "split this string into word pairs please" 输出数组应该是这样的 Array ( [0] = split this [1] = this string [2] = string into [3] = into word [4] = word pairs [5] = pairs plea
                
                
                
            | 
 我试图在 
 PHP中将字符串拆分为单词对数组.例如,如果您有输入字符串: 
  
  
  "split this string into word pairs please" 输出数组应该是这样的 Array (
    [0] => split this
    [1] => this string
    [2] => string into
    [3] => into word
    [4] => word pairs
    [5] => pairs please
    [6] => please
)一些失败的尝试包括: $array = preg_split('/w+s+w+/',$string);这给了我一个空数组,和 preg_match('/w+s+w+/',$string,$array);它将字符串拆分为单词对但不重复单词.是否有捷径可寻?谢谢. 解决方法
 为什么不使用爆炸? 
  
  
  $str = "split this string into word pairs please";
$arr = explode(' ',$str);
$result = array();
for($i=0;$i<count($arr)-1;$i++) {
        $result[] =  $arr[$i].' '.$arr[$i+1];
}
$result[] =  $arr[$i];Working link (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! | 
