并排分解相同字符的函数
Explode function side by side same character
我有一个字符串
$str = 'one,,two,three,,,,,four';
我想这样排列(print_r的输出)
Array ( [0] => one,two,three,four )
我的密码是
$str = 'one,,two,three,,,,,four';
$str_array = explode(',', $str);
print_r($str_array);
但是不工作因为多个逗号并排 side.How 我可以解决这个问题吗?
您可以使用 array_filter 函数从数组中删除空元素。
所以你的代码应该是:
$str = 'one,,two,three,,,,,four';
$str_array = array_filter(explode(',', $str));
print_r($str_array);
已编辑代码
$str = 'one,,two,three,,,,,four';
$str_array = implode(',',array_filter(explode(',', $str)));
echo $str_array; // you will get one,two,three,four
您可以使用 preg_replace
作为
删除多个逗号
$str = 'one,,two,three,,,,,four';
echo $str_new = preg_replace('/,+/', ',', $str);// one,two,three,four
$str_array = explode(' ', $str_new);
print_r($str_array);//Array ( [0] => one,two,three,four )
试试这个
<?php
$string = 'one,,two,three,,,,,four';
$new_array = array_filter(explode(',', $string));
$final_array[] = implode(',',$new_array);
print_r($final_array);
?>
OUTPUT: Array ( [0] => one,two,three,four )
<?php
$string = 'one,,two,three,,,,,four';
$result = array(preg_replace('@,+@', ',', $string));
print_r($result);
输出:
Array ( [0] => one,two,three,four )
我有一个字符串
$str = 'one,,two,three,,,,,four';
我想这样排列(print_r的输出)
Array ( [0] => one,two,three,four )
我的密码是
$str = 'one,,two,three,,,,,four';
$str_array = explode(',', $str);
print_r($str_array);
但是不工作因为多个逗号并排 side.How 我可以解决这个问题吗?
您可以使用 array_filter 函数从数组中删除空元素。
所以你的代码应该是:
$str = 'one,,two,three,,,,,four';
$str_array = array_filter(explode(',', $str));
print_r($str_array);
已编辑代码
$str = 'one,,two,three,,,,,four';
$str_array = implode(',',array_filter(explode(',', $str)));
echo $str_array; // you will get one,two,three,four
您可以使用 preg_replace
作为
$str = 'one,,two,three,,,,,four';
echo $str_new = preg_replace('/,+/', ',', $str);// one,two,three,four
$str_array = explode(' ', $str_new);
print_r($str_array);//Array ( [0] => one,two,three,four )
试试这个
<?php
$string = 'one,,two,three,,,,,four';
$new_array = array_filter(explode(',', $string));
$final_array[] = implode(',',$new_array);
print_r($final_array);
?>
OUTPUT: Array ( [0] => one,two,three,four )
<?php
$string = 'one,,two,three,,,,,four';
$result = array(preg_replace('@,+@', ',', $string));
print_r($result);
输出:
Array ( [0] => one,two,three,four )