如何使用空格和换行来爆炸字符串?

sur*_*190 8 php delimiter

我想爆炸一个字符串(基于分隔符分隔并放入一个数组中),使用空格("")和换行符("\n")作为分隔符.

该方法(我认为不是最好的方法)是:

  1. 用空格分解数组
  2. 从数组中重新生成字符串
  3. 再次爆炸新线
  4. MySQL逃避了各个元素.

问题:如何通过空格和换行来展开字符串?

参考:新行数组

Pao*_*fan 13

你可以做一个

$segments = preg_split('/[\s]+/', $string )
Run Code Online (Sandbox Code Playgroud)

此函数将$string在每个空格(\s)出现时分割,包括空格,制表符和换行符.多个连续的空格将计为一个(例如"hello, \n\t \n\nworld!"将被拆分hello,world!只,中间无空字符串.

见功能参考这里.


Yog*_*har 7

您可以使用preg_split多个分隔符来爆炸内容

$pattern = '/[ \n]/';
$string = "something here ; and there, oh,that's all!";
echo '<pre>', print_r( preg_split( $pattern, $string ), 1 ), '</pre>'; 
Run Code Online (Sandbox Code Playgroud)