I have the following code:
preg_match_all('/(.*) \((\d+)\) - ([\d\.\d]+)[,?]/U',
"E-Book What I Didn't Learn At School... (2) - 3525.01, FREE Intro DVD/Vid (1) - 0.15",
$match);
var_dump($string, $match);
and get the following ouput:
array(4) {
[0]=>
array(1) {
[0]=>
string(54) "E-Book What I Didn't Learn At School... (2) - 3525.01,"
}
[1]=>
array(1) {
[0]=>
string(39) "E-Book What I Didn't Learn At School..."
}
[2]=>
array(1) {
[0]=>
string(1) "2"
}
[3]=>
array(1) {
[0]=>
string(7) "3525.01"
}
}
which matches only one 开发者_运维百科items... what i need is to get all items from such strings. when i've added "," sign to the end of the string - it worked fine. but that is non-sense in adding comma to each string. Any advice?
Try this regex:
(.*?)\s*\((\d+)\)\s*-\s*(\d+\.\d+)(?:,\s*)?
The major difference is that you had .*
(greedy) which I replaced with .*?
(un-greedy). Yours first "ate" the entire string (except line breaks) and then back tracked to match just one piece from your string.
Demo:
preg_match_all('/(.*?)\s*\((\d+)\)\s*-\s*(\d+\.\d+)(?:,\s*)?/',
"E-Book What I Didn't Learn At School... (2) - 3525.01, FREE Intro DVD/Vid (1) - 0.15",
$matches, PREG_SET_ORDER);
print_r($matches);
produces:
Array
(
[0] => Array
(
[0] => E-Book What I Didn't Learn At School... (2) - 3525.01,
[1] => E-Book What I Didn't Learn At School...
[2] => 2
[3] => 3525.01
)
[1] => Array
(
[0] => FREE Intro DVD/Vid (1) - 0.15
[1] => FREE Intro DVD/Vid
[2] => 1
[3] => 0.15
)
)
精彩评论