如何将一个句子转换成一个单词数组? [英] How can I convert a sentence to an array of words?
本文介绍了如何将一个句子转换成一个单词数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
通过此字符串:
$input = "Some terms with spaces between";
我怎么产生这个数组?
$output = ['Some', 'terms', 'with', 'spaces', 'between'];
推荐答案
您可以使用 explode
, split
或 preg_split
.
You could use explode
, split
or preg_split
.
explode
使用固定的字符串:
$parts = explode(' ', $string);
而split
和preg_split
使用正则表达式:
$parts = split(' +', $string);
$parts = preg_split('/ +/', $string);
基于正则表达式的拆分很有用的示例:
An example where the regular expression based splitting is useful:
$string = 'foo bar'; // multiple spaces
var_dump(explode(' ', $string));
var_dump(split(' +', $string));
var_dump(preg_split('/ +/', $string));
这篇关于如何将一个句子转换成一个单词数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文