Php Multiple Delimiters in Explode
Exploding a string with multiple delimiters in PHP can be challenging. To address this, it is possible to define a custom function to perform the task. One such implementation is shown below:
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;
}
However, a more efficient solution is to utilize the preg_split() function. This function allows you to specify multiple delimiters by constructing a regular expression. For example:
$output = preg_split('/ (@|vs) /', $input);
This expression will split the input string on either the "@" or "vs" delimiters, resulting in an array with separate values.
Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.
Copyright© 2022 湘ICP备2022001581号-3