[英]How to split a string by multiple delimiters in PHP?
"something here ; and there, oh,that's all!"
I want to split it by ;
我想分开它
;
and ,
而且
,
so after processing should get: 所以处理后应该得到:
something here
and there
oh
that's all!
<?php
$pattern = '/[;,]/';
$string = "something here ; and there, oh,that's all!";
echo '<pre>', print_r( preg_split( $pattern, $string ), 1 ), '</pre>';
Updated answer to an updated question: 更新了更新问题的答案:
<?php
$pattern = '/[\x{ff0c},]/u';
//$string = "something here ; and there, oh,that's all!";
$string = 'hei,nihao,a ';
echo '<pre>', print_r( preg_split( $pattern, $string ), 1 ), '</pre>';
$result_array = preg_split( "/[;,]/", $starting_string );
The split() PHP function allows the delimiter to be a regular expression. split()PHP函数允许分隔符成为正则表达式。 Unfortunately it's deprecated and will be removed in PHP7!
不幸的是它已被弃用,将在PHP7中删除!
The preg_split() function should be OK, and it returns an array: preg_split()函数应该没问题,它返回一个数组:
$results = preg_split('/[;,]/', $string);
There are a few extra optional parameters which may be useful to you. 还有一些额外的可选参数可能对您有用。
Is the first delimiter character in your edited example actually a 2 byte Unicode character? 您编辑的示例中的第一个分隔符字符实际上是一个2字节的Unicode字符吗?
Perhaps the preg_slit() function is treating the delimiter as three characters and splitting between the characters of the unicode (Chinese?) 'character' 也许preg_slit()函数将分隔符视为三个字符并在unicode(中文?)'字符'的字符之间分割
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.