爆炸功能不在爆炸字符中

发布于 2024-10-19 00:21:01 字数 692 浏览 3 评论 0原文

我正在寻找一个 explode 类型的函数,该函数将按字符分解字符串,但也会获取一个字符列表以忽略该字符是否在其中。

例如:

$str = "hello, this is, a test 'some, string' thanks";
explode_func($str, ",", "'");

这会将 $str 分解为 ,,但忽略 ' 中的任何 ,

预期输出:

Array
(
    [0] => hello
    [1] => this is
    [2] => a test
    [3] => thanks
)

另一个示例是be:

$str = "hello, this is, a test (some, string) thanks";
explode_func($str, ",", "()");

这会将 $str 分解为 ,,但忽略 (), code> 获得相同的输出。

有什么想法吗?

I am looking for an explode type function that will explode a string by characters but also take a list of characters to ignore if the character is within them.

For example:

$str = "hello, this is, a test 'some, string' thanks";
explode_func($str, ",", "'");

This would explode $str by , but ignore any , within '

Expected output:

Array
(
    [0] => hello
    [1] => this is
    [2] => a test
    [3] => thanks
)

Another example would be:

$str = "hello, this is, a test (some, string) thanks";
explode_func($str, ",", "()");

This would explode $str by , but ignore any , between ( and ) to get the same output.

Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

奶气 2024-10-26 00:21:02

str_getcsv()

$str = "hello, this is, a test 'some, string' thanks";
$array = str_getcsv($str, ",", "'");

应该给出:

Array
(
    [0] => hello
    [1] => this is
    [2] => a test 'some, string' thanks
)

str_getcsv()

$str = "hello, this is, a test 'some, string' thanks";
$array = str_getcsv($str, ",", "'");

which should give:

Array
(
    [0] => hello
    [1] => this is
    [2] => a test 'some, string' thanks
)
潜移默化 2024-10-26 00:21:02

最好的选择是首先删除您想要忽略的区域。

function explode($str, $separator, $ignore_pattern) {
    $newStr = preg_replace($ignore_pattern, '', $str);
    return explode($separator, $str);
}

用法:

$str = "hello, this is, a test (some, string) thanks";
$array = explode($str, ',', '/\(.*?\)/');

未经测试,但理论可靠

Your best bet would be to remove the areas you want to ignore first.

function explode($str, $separator, $ignore_pattern) {
    $newStr = preg_replace($ignore_pattern, '', $str);
    return explode($separator, $str);
}

usage:

$str = "hello, this is, a test (some, string) thanks";
$array = explode($str, ',', '/\(.*?\)/');

Not tested but the theory is solid

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文