可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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) multiExplode($delimiters, $string); } return $ary; }
回答1:
what about using
$output = preg_split( "/ (@|vs) /", $input );
回答2:
You can take the first string, replace all the @ with vs using str_replace, then explode on vs or vice versa.
回答3:
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
回答4:
Wouldn't strtok() work for you?
回答5:
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!
回答6:
Simply you can use the following code:
$arr=explode('sep1',str_replace(array('sep2','sep3','sep4'),'sep1',$mystring));
回答7:
You are going to have some problems (what if you have this string: "vs @ apples" for instance) using this method of sepparating, but if we start by stating that you have thought about that and have fixed all of those possible collisions, you could just replace all occurences of $delimiter[1] to $delimiter[n] with $delimiter[0], and then split on that first one?
回答8:
If your delimiter is only characters, you can use strtok, which seems to be more fit here. Note that you must use it with a while loop to achieve the effects.
回答9:
I do it this way...
public static function multiExplode($delims, $string, $special = '|||') { if (is_array($delims) == false) { $delims = array($delims); } if (empty($delims) == false) { foreach ($delims as $d) { $string = str_replace($d, $special, $string); } } return explode($special, $string); }
回答10:
This will work:
$stringToSplit = 'This is my String!' ."\n\r". 'Second Line'; $split = explode ( ' ', implode ( ' ', explode ( "\n\r", $stringToSplit ) ) );
As you can see, it first glues the by \n\r exploded parts together with a space, to then cut it apart again, this time taking the spaces with him.
回答11:
You can try this solution.... It works great
function explodeX( $delimiters, $string ) { return explode( chr( 1 ), str_replace( $delimiters, chr( 1 ), $string ) ); } $list = 'Thing 1&Thing 2,Thing 3|Thing 4'; $exploded = explodeX( array('&', ',', '|' ), $list ); echo ''; print_r($exploded); echo '
';
Reference : http://www.phpdevtips.com/2011/07/exploding-a-string-using-multiple-delimiters-using-php/