replace comma between double quotes using preg_replace [duplicate]
Asked Answered
P

1

2

I have a csv file like this

Number,Name,UG College,UG University,PG College,PG University
1100225,Lakshmin,pkrrrr College,"PKRRRRR University, Choice",nandhaaaaa,"Nandhaa University, Kumali"

While reading the file I want to replace comma only inside double quotes .

Thanks in advance .

Pilgrimage answered 10/5, 2013 at 11:38 Comment(0)
S
2
$string = '1100225,Lakshmin,pkrrrr College,"PKRRRRR University, Choice",nandhaaaaa,"Nandhaa University, Kumali"';

$string = preg_replace_callback(
        '|"[^"]+"|',
        create_function(
            // single quotes are essential here,
            // or alternative escape all $ as \$
            '$matches',
            'return str_replace(\',\',\'*comma*\',$matches[0]);'
        ),$string );
$array = explode(',',$string);
$array = str_replace('*comma*',',',$array);
print_r($array);       
exit;
Saltarello answered 10/5, 2013 at 13:25 Comment(3)
What encoding does it assume the string to be in?Tammitammie
the preg function got a u modifier to set UTF8, see php.net/manual/en/reference.pcre.pattern.modifiers.php other function are binary-safeSaltarello
create_function in php 7.2 is deprecated. Is there any solution about it? thank you in advanceFaggoting

© 2022 - 2024 — McMap. All rights reserved.