如何在PHP中使用多个分隔符拆分字符串?

时间:2021-12-17 21:37:48

"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!

4 个解决方案

#1


33  

<?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>';

#2


8  

$result_array = preg_split( "/[;,]/", $starting_string );

#3


1  

The split() PHP function allows the delimiter to be a regular expression. Unfortunately it's deprecated and will be removed in PHP7!

split() PHP函数允许分隔符是正则表达式。不幸的是,它已被弃用,将在PHP7中删除!

The preg_split() function should be OK, and it returns an array:

preg_split()函数应该是OK,并返回一个数组:

$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_cut()函数将分隔符视为三个字符,并在unicode字符(中文?)之间进行分割。“性格”

#4


0  

You can get the values into an array using Devin's or Meder's method.

您可以使用Devin或Meder的方法将值获取到数组中。

To get the output you want, you could probably do this

要得到想要的输出,您可以这样做

echo implode("\n", $resultingArray);

Or use <br /> if it's HTML you want.

或者使用
(如果是HTML)。

#1


33  

<?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>';

#2


8  

$result_array = preg_split( "/[;,]/", $starting_string );

#3


1  

The split() PHP function allows the delimiter to be a regular expression. Unfortunately it's deprecated and will be removed in PHP7!

split() PHP函数允许分隔符是正则表达式。不幸的是,它已被弃用,将在PHP7中删除!

The preg_split() function should be OK, and it returns an array:

preg_split()函数应该是OK,并返回一个数组:

$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_cut()函数将分隔符视为三个字符,并在unicode字符(中文?)之间进行分割。“性格”

#4


0  

You can get the values into an array using Devin's or Meder's method.

您可以使用Devin或Meder的方法将值获取到数组中。

To get the output you want, you could probably do this

要得到想要的输出,您可以这样做

echo implode("\n", $resultingArray);

Or use <br /> if it's HTML you want.

或者使用
(如果是HTML)。