3v4l.org

run code in 300+ PHP versions simultaneously
<?php $str = '1,"4052","B00K6ED81S",,"Bottle, white - 6,5 l, WENKO","Good design!","Bottle, white 6,5 l, WENKO",,,"item","23",23,"23",23,31.22,31.22,,1,,,,0,8,"4",,0,,0,0,,0,,0,0,0,,'; $pos=1; // set $pos to make sure while loop does not end directly. $newstr = ""; $prevPos = 0; $skip = false; // flag to know if replace should be done or not while($pos != false){ $pos = strpos($str, '"', $prevPos); // find " in string after prevPos $part = substr($str, $prevPos, $pos+1-$prevPos); // substring the part (first time it runs it will be '1,"' then '4052"') if($skip){ // if it's between two " (a string) skip the replace //echo "skip " . $part . "\n"; $skip =!$skip; // change the flag $newstr .= $part; }else{ // if it's not in a string do the replace on the $part //echo "!skip " . $part . "\n"; $newstr .= str_replace(",", ";", $part); $skip =!$skip; // change the flag. } $prevPos = $pos+1; // set new $prevPos } // if the loop ends and there is no more " in the string we need to replace , to ; on the rest of the string. // we know the loop ended at strlen($newstr), so that is the $part. if($pos<strlen($str)) $newstr .= str_replace(",", ";", substr($str, strlen($newstr))); echo $str . "\n"; echo $newstr;
Output for 7.0.0 - 7.0.20, 7.1.0 - 7.1.20, 7.2.6 - 7.2.33, 7.3.16 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.18, 8.3.0 - 8.3.4, 8.3.6
1,"4052","B00K6ED81S",,"Bottle, white - 6,5 l, WENKO","Good design!","Bottle, white 6,5 l, WENKO",,,"item","23",23,"23",23,31.22,31.22,,1,,,,0,8,"4",,0,,0,0,,0,,0,0,0,, 1;"4052";"B00K6ED81S";;"Bottle, white - 6,5 l, WENKO";"Good design!";"Bottle, white 6,5 l, WENKO";;;"item";"23";23;"23";23;31.22;31.22;;1;;;;0;8;"4";;0;;0;0;;0;;0;0;0;;
Output for 8.3.5
Warning: PHP Startup: Unable to load dynamic library 'sodium.so' (tried: /usr/lib/php/8.3.5/modules/sodium.so (libsodium.so.23: cannot open shared object file: No such file or directory), /usr/lib/php/8.3.5/modules/sodium.so.so (/usr/lib/php/8.3.5/modules/sodium.so.so: cannot open shared object file: No such file or directory)) in Unknown on line 0 1,"4052","B00K6ED81S",,"Bottle, white - 6,5 l, WENKO","Good design!","Bottle, white 6,5 l, WENKO",,,"item","23",23,"23",23,31.22,31.22,,1,,,,0,8,"4",,0,,0,0,,0,,0,0,0,, 1;"4052";"B00K6ED81S";;"Bottle, white - 6,5 l, WENKO";"Good design!";"Bottle, white 6,5 l, WENKO";;;"item";"23";23;"23";23;31.22;31.22;;1;;;;0;8;"4";;0;;0;0;;0;;0;0;0;;

preferences:
157.45 ms | 402 KiB | 173 Q