parsing - Separate string by first two whitespaces and comma, PHP -
i have long set of numbers , description followed comma need parse 3 fields, example:
001 009 intestinal infectious diseases,010 018 tuberculosis,020 027 zoonotic bacterial diseases,030 041 other bacterial diseases,
i need insert data before each comma 3 fields in database, start, end , description. 2 examples be:
start: 001; end: 009; description: intestinal infectious diseases start: 010; end: 018; description: tuberculosis
so basically, first space ends start digits, second space ends end digits, , comma ends description.
i unsure how write out can store each record... appreciated.
this should it.
<?php $string = '001 009 intestinal infectious diseases,010 018 tuberculosis,020 027 zoonotic bacterial diseases,030 041 other bacterial diseases,'; preg_match_all('~(\d+)\s(\d+)\s(.*?),~', $string, $matches); for($i =0; $i < count($matches); $i++) { echo 'first integer: ' . $matches[1][$i] . 'second integer: ' . $matches[2][$i] . 'description: ' . $matches[3][$i] . "\n"; }
this regex looking 1 whitespace between values. if should @ least 1 whitespace character add +
. \d+
@ least 1 number , many continuos numbers until white space. .*?
until first comma.
output:
first integer: 001second integer: 009description: intestinal infectious diseases
first integer: 010second integer: 018description: tuberculosis
first integer: 020second integer: 027description: zoonotic bacterial diseases
first integer: 030second integer: 041description: other bacterial diseases
Comments
Post a Comment