I have a text file and want to split the text into array using regular expression. But I am new to regex and don't know how to use it. The text file format is basically like this:
0,"20"1,"100000050"25,"100000050"19,""11,"Masuda"12,"Jin"
I want to split them like:
0: 0,"20"
1: 1,"100000050"
2: 25,"100000050"
...
Please help! Any answer would be appreciated!
Use the preg_split() function. It operates exactly like split(), except that regular expressions are accepted as input parameters for pattern.
Using PREG_SPLIT_DELIM_CAPTURE
returns the parenthesized expression in the delimiter pattern.
preg_split(
'/([\d]+,\"[0-9a-zA-Z]+\")/',
$str,
-1,
PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY
);
/([\d]+,\"[0-9a-zA-Z]+\")/
is the regular expression.
/ = start or end of pattern string
[ ... ] = grouping of characters
\d - digits
+ = one or more of the preceeding character or group
, = the literal comma character
\" = the literal quote character
[0-9a-zA-Z] = numbers and letters
That seems like a weird formatting so I might miss something, but this should work:
([0-9]+,\"([0-9a-z ]+)?\")
Details
[0-9]+ match a digit one or more times (this seems to be an ID of sorts)
, match a literal comma
\"([0-9a-z ]+)?\" match an alphanumeric character or a space one or more times, optionally (you have an empty string), between quotes
i flag to make it case insensitive
Pair it with preg_match_all()
to get all the matches in an array:
<?php
$string = '0,"20"1,"100000050"25,"100000050"19,""11,"Masuda"12,"Jin"';
preg_match_all("/([0-9]+,\"([0-9a-z]+)?\")/i", $string, $m);
var_dump($m);
The first array will have what you need.