Explode string by one or more spaces or tabs
$parts = preg_split('/\s+/', $str);
php preg_split explode line by multiple spaces and tabs in fields that may contain spaces too
You can use str_getcsv()
<?php
$line = 'A 4 "AB5672HMKL OLD" B 9500 8150 39 0000 L XFN "ProductPN"';
print_r(str_getcsv($line, ' '));
Output:-https://3v4l.org/6Qs5e
How can I explode a string by more than one space, but not by exactly one space in php
Just use preg_split('/ +/', $line);
-- this is a blank followed by one or more blanks. Note that there are 2 blanks between the / and the +, even if it looks like one. Or, you could write it as '/ {2,}/'
, which also means previous expression (the blank) repeated at least 2 times.
Split string into substrings on one or more whitespaces
Try
strsplit(test, '\\s+')
[[1]]
[1] "123" "nnn" "dddddd"
\\s
will match all the whitespace characters.
In Ruby, how do I split on two or more spaces or a tab?
You could try this:
"a\t\tb c d".split(/\t| {2,}/)
#=> ["a", "", "b", "c d"]
"ab \t\t\tf".split(/\t| {2,}/)
#=> ["ab ", "", "", "f"]
Where \t
is for a tab and {2,}
for two or more spaces. Notice that there is a space before {2,}
.
To include non-breaking spaces you could add \u00A0
to the expression, like this:
str.split(/\t|[ |\u00A0]{2,}/)
Examples:
str = "a\t\tb \u00A0 c d" #=> "a\t\tb c d"
str.split(/\t|[ |\u00A0]{2,}/) #=> ["a", "", "b", "c d"]
str = "ab \t\t\tf" #=> "ab \t\t\tf"
str.split(/\t|[ |\u00A0]{2,}/) #=> ["ab ", "", "", "f"]
Where [ |\u00A0]{2,}
will check for 2 or more occurrences of either a space or non-breaking space.
Splitting a string with multiple spaces
Since the argument to split()
is a regular expression, you can look for one or more spaces (" +"
) instead of just one space (" "
).
String[] array = s.split(" +");
Explode string on second last and last space to create exactly 3 elements
You can approach this using explode
and str_replace
$string = "Testing (3WIR)";
$stringToArray = explode(":",str_replace("(",":(",$string));
echo '<pre>';
print_r($stringToArray);
Edited question answer:-
$subject = "Fluency in English Conversation Defklmno (1WIR)";
$toArray = explode(' ',$subject);
if(count($toArray) > 2){
$first = implode(" ",array_slice($toArray, 0,count($toArray)-2));
$second = $toArray[count($toArray)-2];
$third = $toArray[count($toArray)-1];
$result = array_values(array_filter([$first, $second, $third]));
}else{
$result = array_values(array_filter(explode(":",str_replace("(",":(",$subject))));
}
DEMO HERE
How do I split on multiple white space or tabs?
It happens because the group you used is a capturing one. See split
reference:
If pattern contains groups, the respective matches will be returned in the array as well.
Use a non-capturing group (used only for grouping patterns) to avoid adding matched strings into the resulting array:
a.strip.split(/(?:[[:space:]][[:space:]]+|\t)/)
^^
Related Topics
Read Each Line of Txt File to New Array Element
Convert Timestamp to Readable Date/Time PHP
Super Fast Getimagesize in PHP
How to Validate Google Recaptcha V3 on Server Side
Why Does the PHP Json_Encode Function Convert Utf-8 Strings to Hexadecimal Entities
Intl Extension: Installing PHP_Intl.Dll
"Vertical" Regex Matching in an Ascii "Image"
How to Get the Current Page'S Full Url on a Windows/Iis Server
Can You Store a Function in a PHP Array
PHP - Merging Two Arrays into One Array (Also Remove Duplicates)
How to Check If a Word Is Contained in Another String Using PHP
Set Cookie Wih Js, Read With PHP Problem
Why Is Register_Globals So Bad
Composer Warning: Openssl Extension Is Missing. How to Enable in Wamp