Skip to content

Instantly share code, notes, and snippets.

@voeurnchy
Last active July 31, 2020 08:54
Show Gist options
  • Save voeurnchy/5fdcdeb7ad403026a0cd076be72e0c7b to your computer and use it in GitHub Desktop.
Save voeurnchy/5fdcdeb7ad403026a0cd076be72e0c7b to your computer and use it in GitHub Desktop.
Explode string with multiple delimiters
<?php
/**
* Explode with multiple delimiters
* @param array $delimiters
* @param string $string
* @return array
*/
function explodeX(array $delimiters, string $string )
{
return explode( chr( 1 ), str_replace( $delimiters, chr( 1 ), $string ) );
}
$string = "banana,apple:orange-salad";
var_dump(explodeX([':', ',', '-'], $string) );
// result:
// array(4) {
// [0]=>
// string(6) "banana"
// [1]=>
// string(5) "apple"
// [2]=>
// string(6) "orange"
// [3]=>
// string(5) "salad"
// }
@gowrishankarbalu
Copy link

I've tried and getting an Fatal error. Refer to the log message below:

PHP Catchable fatal error: Argument 2 passed to explodeX() must be an instance of string, string given, called in test.php on line 16 and defined in test.php on line 10

update your script.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment