首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何在PHP中按多个分隔符拆分字符串?

如何在PHP中按多个分隔符拆分字符串?
EN

Stack Overflow用户
提问于 2009-09-21 03:30:54
回答 4查看 42.2K关注 0票数 30

"something here ; and there, oh,that's all!"

我想把它分为;,

所以在处理之后应该得到:

代码语言:javascript
复制
something here

and there

oh

that's all!
EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2009-09-21 03:35:38

代码语言:javascript
复制
<?php

$pattern = '/[;,]/';

$string = "something here ; and there, oh,that's all!";

echo '<pre>', print_r( preg_split( $pattern, $string ), 1 ), '</pre>';

已更新问题的更新答案:

代码语言:javascript
复制
<?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>';
票数 45
EN

Stack Overflow用户

发布于 2009-09-21 03:34:55

代码语言:javascript
复制
$result_array = preg_split( "/[;,]/", $starting_string );
票数 16
EN

Stack Overflow用户

发布于 2009-09-21 03:39:24

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

preg_split()函数应该没问题,它返回一个数组:

代码语言:javascript
复制
$results = preg_split('/[;,]/', $string);

还有一些额外的可选参数可能对您有用。

您编辑的示例中的第一个分隔符字符实际上是一个2字节的Unicode字符吗?

也许preg_slit()函数将分隔符视为三个字符,并在unicode (中文?)‘字符’

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/1452777

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档