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>';
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.
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)
)
Split String by Multiple Delimiters in PHP
PHP
$str = "a,b c,d;e f";
$pieces = preg_split('/[, ;]/', $str);
var_dump($pieces);
CodePad.
Output
array(6) {
[0]=>
string(1) "a"
[1]=>
string(1) "b"
[2]=>
string(1) "c"
[3]=>
string(1) "d"
[4]=>
string(1) "e"
[5]=>
string(1) "f"
}
How to split string on multiple delimiters keeping some delimiters?
Try this:
$src='word1&word2 !word3 word4 &word5';
$arr=explode(' ',$src=preg_replace('/(?<=[\w])([&!])/',' $1',$src));
echo join('<br>',$arr); // present the result ...
First change any occurence of a group consisting of a single character of class [&!]
that is preceded by a 'word'-character into ' $1'
(=itself, preceded with a blank) and then explode()
the string using the blanks as separators.
If you need to deal with multiple blanks as separators between the words you could of course replace the (faster) explode(' ',...)
with a slighty more "refined" preg_split('/ +/',...)
.
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);
Php multiple delimiters in explode
Try about using:
$output = preg_split('/ (@|vs) /', $input);
Split string into associative array by multiple delimiters
With the help of Your Common Sense and CBroe I figured it out:
$result = [];
$pattern = '/(\{\{[^}]*\}\})/';
$lines = preg_split( $pattern, $structure, null, PREG_SPLIT_DELIM_CAPTURE );
foreach ( $lines as $line ) {
preg_match( $pattern, $line, $matches );
if ( $matches ) {
$result[] = [
'type' => 'dropdown',
'name' => trim( str_replace( ['{{', '}}'], "", $line ) )
];
} else {
$result[] = [
'type' => "text",
'content' => trim( $line )
];
}
}
Related Topics
Import CSV File Directly into MySQL
How to Create Multilingual Translated Routes in Laravel
Rebase Array Keys After Unsetting Elements
Pdo With "Where... In" Queries
How to Receive Email and Process It in a Web Application
Regular Expression Pattern to Match Url With or Without Http://Www
PHP, MySQL Error: Column Count Doesn't Match Value Count At Row 1
Upstream Sent Too Big Header While Reading Response Header from Upstream
How to Get Everything After a Certain Character
Best Way to Check If MySQL_Query Returned Any Results
PHP: How to Get File Creation Date
Find Out How PHP Is Running on Server (Cgi or Fastcgi or Mod_PHP)