php 中的多个分隔符爆炸

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4955433/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 16:21:28  来源:igfitidea点击:

Php multiple delimiters in explode

phpexplode

提问by OHLáLá

I have a problem, I have a string array, and I want to explode in different delimiter. For Example

我有一个问题,我有一个字符串数组,我想在不同的分隔符中爆炸。例如

$example = 'Appel @ Ratte';
$example2 = 'apple vs ratte'

and I need an array which is explode in @ or vs.

我需要一个在@ 或 vs 中爆炸的数组。

I already wrote a solution, but If everybody have a better solution please post here.

我已经写了一个解决方案,但是如果每个人都有更好的解决方案,请在此处发布。

private function multiExplode($delimiters,$string) {
    $ary = explode($delimiters[0],$string);
    array_shift($delimiters);
    if($delimiters != NULL) {
        if(count($ary) <2)                      
            $ary = $this->multiExplode($delimiters, $string);
    }
    return  $ary;
}

回答by SergeS

what about using

怎么样使用

$output = preg_split( "/ (@|vs) /", $input );

回答by John Ballinger

You can take the first string, replace all the @with vsusing str_replace, then explode on vsor vice versa.

你可以取第一个字符串,@vsusing替换所有的str_replace,然后爆炸,vs反之亦然。

回答by bizauto

function multiexplode ($delimiters,$string) {

    $ready = str_replace($delimiters, $delimiters[0], $string);
    $launch = explode($delimiters[0], $ready);
    return  $launch;
}

$text = "here is a sample: this text, and this will be exploded. this also | this one too :)";


$exploded = multiexplode(array(",",".","|",":"),$text);

print_r($exploded);

//And output will be like this:
// Array
// (
//    [0] => here is a sample
//    [1] =>  this text
//    [2] =>  and this will be exploded
//    [3] =>  this also 
//    [4] =>  this one too 
//    [5] => )
// )

Source: php@metehanarslan at php.net

资料来源:php@metehanarslan 在 php.net

回答by Aubry

How about using strtr()to substitute all of your other delimiters with the first one?

如何使用strtr()替代所有其他的分隔符与第一个?

private function multiExplode($delimiters,$string) {
    return explode(
        $delimiters[0],
        strtr(
            $string,
            array_combine(
                array_slice(    $delimiters, 1  ),
                array_fill(
                    0,
                    count($delimiters)-1,
                    array_shift($delimiters)
                )
            )
        )
    );
}

It's sort of unreadable, I guess, but I tested it as working over here.

我想这有点不可读,但我在这里测试了它。

One-liners ftw!

单线 ftw!

回答by Mchl

Wouldn't strtok()work for you?

strtok()适合你吗?

回答by Samer Ata

Simply you can use the following code:

只需您可以使用以下代码:

$arr=explode('sep1',str_replace(array('sep2','sep3','sep4'),'sep1',$mystring));

回答by Mayur Chauhan

You can try this solution.... It works great

你可以试试这个解决方案......效果很好

function explodeX( $delimiters, $string )
{
    return explode( chr( 1 ), str_replace( $delimiters, chr( 1 ), $string ) );
}
$list = 'Thing 1&Thing 2,Thing 3|Thing 4';

$exploded = explodeX( array('&', ',', '|' ), $list );

echo '<pre>';
print_r($exploded);
echo '</pre>';

Source : http://www.phpdevtips.com/2011/07/exploding-a-string-using-multiple-delimiters-using-php/

来源:http: //www.phpdevtips.com/2011/07/exploding-a-string-using-multiple-delimiters-using-php/

回答by A.F.

How about this?

这个怎么样?

/**
 * Like explode with multiple delimiters. 
 * Default delimiters are: \ | / and ,
 *
 * @param string $string String that thould be converted to an array.
 * @param mixed $delimiters Every single char will be interpreted as an delimiter. If a delimiter with multiple chars is needed, use an Array.
 * @return array. If $string is empty OR not a string, return false
 */
public static function multiExplode($string, $delimiters = '\|/,') 
{
  $delimiterArray = is_array($delimiters)?$delimiters:str_split($delimiters);
  $newRegex = implode('|', array_map (function($delimiter) {return preg_quote($delimiter, '/');}, $delimiterArray));
  return is_string($string) && !empty($string) ? array_map('trim', preg_split('/('.$newRegex.')/', $string, -1, PREG_SPLIT_NO_EMPTY)) : false;
}

In your case you should use an Array for the $delimiters param. Then it is possible to use multiple chars as one delimiter.

在您的情况下,您应该使用数组作为 $delimiters 参数。那么就可以使用多个字符作为一个分隔符。

If you don't care about trailing spaces in your results, you can remove the array_map('trim', [...] )part in the return row. (But don't be a quibbler in this case. Keep the preg_splitin it.)

如果您不关心结果中的尾随空格,则可以删除array_map('trim', [...] )返回行中的部分。(但在这种情况下不要吹毛求疵。把preg_split它留在里面。)

Required PHP Version: 5.3.0 or higher.

所需的 PHP 版本:5.3.0 或更高。

You can test it here

你可以在这里测试

回答by Vaci

I do it this way...

我是这样做的...

public static function multiExplode($delims, $string, $special = '|||') {

    if (is_array($delims) == false) {
        $delims = array($delims);
    }

    if (empty($delims) == false) {
        foreach ($delims as $d) {
            $string = str_replace($d, $special, $string);
        }
    }

    return explode($special, $string);
}

回答by Nanne

You are going to have some problems (what if you have this string: "vs @ apples" for instance) using this method of sepparating, but if we start by stating that you have thought about that and have fixed all of those possible collisions, you could just replace all occurences of $delimiter[1]to $delimiter[n]with $delimiter[0], and then split on that first one?

使用这种分离方法会遇到一些问题(例如,如果您有这个字符串:“vs @ apples”),但是如果我们首先声明您已经考虑过并修复了所有可能的冲突,你可以只更换出现的所有$delimiter[1]$delimiter[n]$delimiter[0],然后对第一个分裂?