Php multiple delimiters in explode
Try about using:
$output = preg_split('/ (@|vs) /', $input);
exploding string with multiple delimiter
Tokens all the way down...
<?php
$str = '1-a,2-b,3-c';
$token = '-,';
if($n = strtok($str, $token))
$array[$n] = strtok($token);
while($n = strtok($token))
$array[$n] = strtok($token);
var_export($array);
Output:
array (
1 => 'a',
2 => 'b',
3 => 'c',
)
Or perhaps more terse without the first if...:
$array = [];
while($n = $array ? strtok($token) : strtok($str, $token))
$array[$n] = strtok($token);
explode() on multiple delimiters
for($i=0;$i<count($lol);$i++){
$lol[$i] = preg_split("@(\s*and\s*)?[/\s,&]+@", $lol[$i]);
}
How can I explode a string by multiple delimiters and also keep the delimiters?
This should work for you:
Just use preg_split()
and set the flags to keep the delimiters. E.g.
<?php
$string = "(2.8 , 3.1) → (↓↓2.4 , ↓3.0)";
$arr = preg_split("/(↑↑|↑|↓↓|↓)/", $string, -1, PREG_SPLIT_DELIM_CAPTURE);
print_r($arr);
?>
output:
Array
(
[0] => (2.8 , 3.1) → (
[1] => ↓↓
[2] => 2.4 ,
[3] => ↓
[4] => 3.0)
)
Can we do multiple explode statements on one line in PHP?
If you're looking to split the string with multiple delimiters, perhaps preg_split
would be appropriate.
$parts = preg_split( '/(\s|&|,)/', 'This and&this and,this' );
print_r( $parts );
Which results in:
Array (
[0] => This
[1] => and
[2] => this
[3] => and
[4] => this
)
explode multiple delimiters in php
I would go for preg_split like below
<?php
$list = "{flower},{animals},{food},{people},{trees}";
$array = preg_split('/[},{]/', $list, 0, PREG_SPLIT_NO_EMPTY);
print_r($array);
?>
The output is
Array
(
[0] => flower
[1] => animals
[2] => food
[3] => people
[4] => trees
)
How to split a string by multiple delimiters in PHP?
<?php
$pattern = '/[;,]/';
$string = "something here ; and there, oh,that's all!";
echo '<pre>', print_r( preg_split( $pattern, $string ), 1 ), '</pre>';
PHP explode string into array with space delimiter when string has multiple spaces?
You can solve your problem and simplify the code by using preg_split()
. You can apply a regex including all delimiters in a character class with a quantifier and \s*
to consume whitespaces around the delimiters.
Code:
$array = preg_split("/\s*[" . preg_quote(implode("", $delimiters), "/") . "]+\s*/", $str);
└┬┘└────────────────────────┬─────────────────────────┘└┬┘
Consuming any whitespaces │ Consuming any whitespaces
around delimiter │ around delimiter
┌──────────────┘
├ [] → Character class with quantifier
├ implode() → Converting array into a string
└ preg_quote() → Escaping special characters
Split string with multiple delimiters
I'd use regexp like this in your case:
preg_split('/,? ?and | ?[,;] ?/', $str)
You may also want to replace spaces by \s
if the other space characters may appear (like TAB, for example) or even \s*
instead of ?
to cover the case of multiple spaces.
Related Topics
Who Should Handle the Conditions in Complex Queries, the Data Mapper or the Service Layer
PHP Library For Parsing Xml With a Colons in Tag Names
Amazon S3 - How to Fix 'The Request Signature We Calculated Does Not Match the Signature' Error
Notice: Trying to Get Property of Non-Object Error
Connecting to Ws-Security Protected Web Service With PHP
How to Properly Escape HTML Form Input Default Values in PHP
Trying to Get Property of Non-object In
How to Login in With Curl and Ssl and Cookies
How to Access a Property With an Invalid Name
Best Way to Do Multiple Constructors in PHP
Reference - Composer Error "Your PHP Version Does Not Satisfy Requirements" After Upgrading PHP
How to Combine Two Arrays Together
Simple PHP Strpos Function Not Working, Why
Creating a Thumbnail from an Uploaded Image