Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,945 questions

51,887 answers

573 users

How to break a string with multiple delimiters into array of values in PHP

3 Answers

0 votes
function multiple_explode($delimiters = array(), $s = '')
{
    foreach($delimiters as $delimiter)
        $s = str_replace($delimiter, $delimiters[0], $s); 
        // can be $delimiters[1] ... it doesn't metter
  
    return explode($delimiters[0], $s);
}
  
$delimiters = array("//", "/", ":", ".", "?", "&", "\"", " ", "-", "\r", "\n", "_", "|");
  
$s = "aaa : bbb / ccc . ddd ? eee&fff - ggg_hhh | iii \n jjj";
  
$arr = multiple_explode($delimiters, $s);
  
echo "<pre>";
print_r($arr);
echo "<pre>";
 
 
/*
run:
     
Array
(
    [0] => aaa
    [1] => 
    [2] => 
    [3] => bbb
    [4] => 
    [5] => 
    [6] => ccc
    [7] => 
    [8] => 
    [9] => ddd
    [10] => 
    [11] => 
    [12] => eee
    [13] => fff
    [14] => 
    [15] => 
    [16] => ggg
    [17] => hhh
    [18] => 
    [19] => 
    [20] => iii
    [21] => 
    [22] => 
    [23] => jjj
)
 
       
*/



answered May 28, 2014 by avibootz
edited Jul 19, 2016 by avibootz
0 votes
function multiple_explode($delimiters = array(), $s = '')
{
    foreach($delimiters as $delimiter)
        $s = str_replace($delimiter, $delimiters[0], $s); 
        // can be $delimiters[1] ... it doesn't metter
 
    return explode($delimiters[0], $s);
}
 
$delimiters = array("//", "/", ":", ".", "?", "&", "\"", " ", "-", "\r", "\n", "_", "|");
 
$s = "aaa : bbb / ccc . ddd ? eee&fff - ggg_hhh | iii \n jjj";
 
$arr = multiple_explode($delimiters, $s);
 
echo "<pre>";
print_r($arr);
echo "<pre>";

$len = sizeof($arr);
for ($i = 0; $i < $len; $i++)
{
    if (isset($arr[$i]))
        if (empty($arr[$i]))
            unset($arr[$i]);
}

echo "<pre>";
print_r($arr);
echo "<pre>";


/*
run:
    
Array
(
    [0] => aaa
    [1] => 
    [2] => 
    [3] => bbb
    [4] => 
    [5] => 
    [6] => ccc
    [7] => 
    [8] => 
    [9] => ddd
    [10] => 
    [11] => 
    [12] => eee
    [13] => fff
    [14] => 
    [15] => 
    [16] => ggg
    [17] => hhh
    [18] => 
    [19] => 
    [20] => iii
    [21] => 
    [22] => 
    [23] => jjj
)
Array
(
    [0] => aaa
    [3] => bbb
    [6] => ccc
    [9] => ddd
    [12] => eee
    [13] => fff
    [16] => ggg
    [17] => hhh
    [20] => iii
    [23] => jjj
)
      
*/

 



answered Jul 19, 2016 by avibootz
0 votes
function multiple_explode($delimiters = array(), $s = '')
{
    foreach($delimiters as $delimiter)
        $s = str_replace($delimiter, $delimiters[0], $s); 
        // can be $delimiters[1] ... it doesn't metter
 
    return explode($delimiters[0], $s);
}
 
$delimiters = array("//", "/", ":", ".", "?", "&", "\"", " ", "-", "\r", "\n", "_", "|");
 
$s = "aaa : bbb / ccc . ddd ? eee&fff - ggg_hhh | iii \n jjj";
 
$arr = multiple_explode($delimiters, $s);
 
echo "<pre>";
print_r($arr);
echo "<pre>";

$len = sizeof($arr);
for ($i = 0; $i < $len; $i++)
{
    if (isset($arr[$i]))
        if (empty($arr[$i]))
            unset($arr[$i]);
}

echo "<pre>";
print_r($arr);
echo "<pre>";

// reindex arr - if you need it, not a must
$arr = array_values($arr);
 
echo "<pre>";
print_r($arr);
echo "<pre>";


/*
run:
    
Array
(
    [0] => aaa
    [1] => 
    [2] => 
    [3] => bbb
    [4] => 
    [5] => 
    [6] => ccc
    [7] => 
    [8] => 
    [9] => ddd
    [10] => 
    [11] => 
    [12] => eee
    [13] => fff
    [14] => 
    [15] => 
    [16] => ggg
    [17] => hhh
    [18] => 
    [19] => 
    [20] => iii
    [21] => 
    [22] => 
    [23] => jjj
)
Array
(
    [0] => aaa
    [3] => bbb
    [6] => ccc
    [9] => ddd
    [12] => eee
    [13] => fff
    [16] => ggg
    [17] => hhh
    [20] => iii
    [23] => jjj
)
Array
(
    [0] => aaa
    [1] => bbb
    [2] => ccc
    [3] => ddd
    [4] => eee
    [5] => fff
    [6] => ggg
    [7] => hhh
    [8] => iii
    [9] => jjj
)
    
*/

 



answered Jul 19, 2016 by avibootz

Related questions

1 answer 182 views
2 answers 175 views
2 answers 197 views
1 answer 170 views
1 answer 114 views
...