Php multiple delimiters in explode

php explode
php explode string multiple delimiters
php explode multiple array
preg_split multiple delimiters
php explode delimiter multiple characters
implode php
multiexplode php
php preg_split

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.

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;
}

what about using

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

Php multiple delimiters in explode, Just a quick note here. To explode() a string using multiple delimiters in PHP you will have to make use of the regular expressions. Use pipe. Using preg_split() to explode() by multiple delimiters in PHP Just a quick note here. To explode ( ) a string using multiple delimiters in PHP you will have to make use of the regular expressions.

You can take the first string, replace all the @ with vs using str_replace, then explode on vs or vice versa.

Using preg_split() to explode() by multiple delimiters in PHP, Here is my approach to have exploded output with multiple delimiter. <?php //$ delimiters has to be array //$string has to be array function multiexplode� Home / PHP explode() with multiple delimiters PHP explode() with multiple delimiters. Simple use this code.

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

explode - Manual, PHP explode() with multiple delimiters. Simple use this code function multiexplode ($delimiters,$data) { $MakeReady = str_replace($delimiters, $ delimiters[0],� Php multiple delimiters in explode . Posted by: admin November 10, 2017 Leave a comment. Questions: I have a problem, I have a string array, and I want to explode in

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

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!

PHP explode() with multiple delimiters – Build your site now, Php multiple delimiters in explode? private function multiExplode($delimiters,$ string) { return explode($delimiters[0],strtr($string,array_combine(� Multi Explode Multiple Delimiters Pass In PHP - Php multiple delimiters in explode multi explode in php explode() preg_split multiple delimiters preg_split examples preg_split php explode multiple delimiters preg_split empty php 7 preg_split split string by regex php preg_match split, preg_split_offset_capture php explode multiple array preg_split multiple delimiters multiexplode php preg

Wouldn't strtok() work for you?

Php multiple delimiters in explode?, Multiple delimiters in PHP Explode Function. See the following code. <?php // app.php function multipleexplode ($delimiters,$string) { $phase� Explode does not parse a string by delimiters, in the sense that we expect to find tokens between a starting and ending delimiter, but instead splits a string into parts by using a string as the boundary of each part.

PHP Explode Function Example, Here i we are writing two type of method to explode any string passing through multiple delimiters. Example 1. Php multiple delimiters in explode. Return Value: Returns an array of strings: PHP Version: 4+ Changelog: The limit parameter was added in PHP 4.0.1, and support for negative limits were added in PHP 5.1.0

Multi Explode Multiple Delimiters Pass In PHP, To explode() a string using multiple delimiters in PHP you will have to make use of the regular expressions. Use pipe. To explode a string using multiple delimiters� To explode multiple delimiters PHP you have to use regular expression. Add your delimiters in the parentheses separated by pipe. You might have to escape your characters if they have meaning in PCRE regular expression syntax.

Php multiple delimiters in explode, 2. delimiters: a string with all wanted delimiters written one after the other e.g. "-'" 3. encoding: Is the character encoding. If it is omitted, the internal character encoding value will be used.

Comments
  • I never saw a question with people giving answers like "How about this", "What about that"
  • Amazing. i wonder how this chalks up against explode() on single arguments
  • It doesn't matter, if you want to make large system with hard string parsing, the most effective way is own parser. Oterwise it doesn't have much effect on system speed, but you preffer to use easiest way for you (To be money effective)
  • Note that some characters needs to be escaped to work (such as /, ?). For example: preg_split("/(\/|\?|=)/", $foo);.
  • preg_split returns indexed array. It possible to return associative? For example $str='/x/1/2/3/4/5/y/100/200/z/777'; preg_split("/(x|y|z|)/", $str); and to see array('x'=>'1/2/3/4/5', 'y'=>'100/200', 'z'=>'777')
  • I found having the spaces in between the / ( and ) / stopped preg_split from working. I had to remove the spaces and then it worked as expected (using PHP 7)
  • Easy solution, ended up using it for Json code to replace "}}" by "|". Dirty but efficient :) !
  • what a genius :D
  • this won't work properly when one delimiter is a superset of the other (e.g. (@|@!) case)
  • @vaxquis Yes it will. str_replace('@!', '@',$str); Make sure you start with the most unique delimiter.
  • @JohnBallinger No, it won't - you proposed another solution, which clearly shows the original solution won't work properly and that your idea is flawed. Imagine you don't know which one is the superset of another. You'd have to create another line of code to check for that etc. While I hate regexes in general, this is a case when it's the best solution in terms of maintainability and simplicity.
  • seeing his delimitter is 'vs', this may not work as php docu says: strtok() splits a string (str) into smaller strings (tokens), with each token being delimited by any character from token. and 'vs' contains two characters
  • this would be a better answer if you included an explanation or example. as it is, it's more of a comment...
  • simple and neat
  • this is what i was looking for