Last active
July 31, 2020 08:54
-
-
Save voeurnchy/5fdcdeb7ad403026a0cd076be72e0c7b to your computer and use it in GitHub Desktop.
Explode string with multiple delimiters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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" | |
// } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.