1

Is there any way I can designate a string delimiter as being any character from an array? For example:

 $delimiter = range("a","z");
 $str = "103a765g678d6f76h";
 $newstr = $explode($delimiter, $str);

Resulting in $newstr being [103,765,678,6,76]

I couldn't find anything on google on how to do this nor could I think of anything myself

BlueMonkMN
  • 25,079
  • 9
  • 80
  • 146
Bobdinator
  • 137
  • 1
  • 1
  • 5

5 Answers5

3

You can use preg_split and a regular expression to achieve what you want.

The implode() is only neccessary if you must range() your desired characters in an array first, it simply concatenates the array elements together to make a string.

$delimiter = range("a","z");

$chars = implode($delimiter);
$str = "103a765g678d6f76h";
$newstr = preg_split("/[$chars]+/", $str, -1, PREG_SPLIT_NO_EMPTY);

Demo

George
  • 36,413
  • 9
  • 66
  • 103
1

Use this

function multiexplode ($delimiters,$string) {

    $ready = str_replace($delimiters, $delimiters[0], $string);
    $launch = explode($delimiters[0], $ready);
    return  $launch;
}
 $delimiter = range("a","z");
     $str = "103a765g678d6f76h";

$text = "here is a sample: this text, and this will be exploded. this also | this one too :)";
$newstr = multiexplode($delimiter ,$str );

print_r($newstr);
Harutyun Abgaryan
  • 2,013
  • 1
  • 12
  • 15
1

Take a look at your data and use preg_replace to replace the range of characters with a single delimiter character. Then explode the modified string.

untitled90
  • 98
  • 5
0

From the PHP explode page this should work for you very well... Doesn't limit it to letters.

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);

Delimiters needs to be an array.

Read more on the PHP site...

Cayce K
  • 2,288
  • 1
  • 22
  • 35
-1

Yes use:

preg_split()

delimiter = range("/a-z/");
 $str = "103a765g678d6f76h";
 $newstr =  preg_split($delimiter, $str);
Edward Manda
  • 559
  • 3
  • 7