PHP Regex Multiple Instances -


i'm importing csv data , need nice , arrayed out.

smaller example data follows.

"name","address" "john doe","5111 fury rd santa cruz" "jane doe","321 tess st texas" "josh doe","653 1st st  orlando florida united states" 

as can see need split on line breaks outside of quotes str_getcsv isn't multi-line.

i had used expression.

$lines = preg_split('/[\r\n]{1,2}(?=(?:[^\"]*\"[^\"]*\")*(?![^\"]*\"))/',$data); 

however preg_split crapped bed when on xxxx amount of characters in string.

so resorting preg_match_all need issues regex selector.

preg_match_all('/^(.*?)[\r\n]{1,2}(?=(?:[^\"]*\"[^\"]*\")*(?![^\"]*\"))/', $data, $matches); 

currently matches first instance.

array(     [0] => array ( [0] => "name","address")     [1] => array ( [0] => "name","address") ) 

any clue return data in array?

here 1 way parse it. have commented out part deletes new lines in address. if want remove commenting.

$re = '/\"(.*?)\",\"(.*?)\"/s'; $data = '"name","address" "john doe","5111 fury rd santa cruz" "jane doe","321 tess st texas" "josh doe","653 1st st orlando florida united states"';  preg_match_all($re, $data, $matches);  /* foreach($matches[2] &$value){     $value = str_replace(php_eol, " ", $value); } */ var_dump($matches); 

https://3v4l.org/7krdt

output foreach:

array(3) {   [0]=>   array(4) {     [0]=>     string(16) ""name","address""     [1]=>     string(36) ""john doe","5111 fury rd santa cruz""     [2]=>     string(30) ""jane doe","321 tess st texas""     [3]=>     string(53) ""josh doe","653 1st st orlando florida united states""   }   [1]=>   array(4) {     [0]=>     string(4) "name"     [1]=>     string(8) "john doe"     [2]=>     string(8) "jane doe"     [3]=>     string(8) "josh doe"   }   [2]=>   array(4) {     [0]=>     string(7) "address"     [1]=>     string(23) "5111 fury rd santa cruz"     [2]=>     string(17) "321 tess st texas"     [3]=>     &string(40) "653 1st st orlando florida united states"   } } 

Comments

Popular posts from this blog

PHP and MySQL WP -

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

go - golang pprof for c library code -