title: PHP str_replace exceptions
body: <p>I have some customer data that i convert from CSV to json using a php script.</p>
  
  <p>once converted the data is all jumbled together, like this:</p>
  
  <p>[{"Customer Number":"12345","Customer Name":"customer 1","Address Code":"PRIMARY","Salesperson ID":"aa","Ship to Address 1":"12345","Ship to Address 2":"123, fake street","Ship to Address 3":"","Ship to City":"Toronto","Ship to Province":"ONTARIO","Ship to Postal Code":"A1A 1A1","Country Code":"CA","Ship to Country":"CANADA","Ship to Contact Person":"John Doe","Ship to Phone":"416-555-5555","Ship to Fax":"416-555-5552","Shipping Method":"OUR TRUCK","Bill to Address 1":"12345","Bill to Address 2":"123, fake street","Bill to Address 3":"","Bill to City":"Toronto","Bill to Province":"ONTARIO","Bill to Postal Code":"A1A 1A1","Bill to Phone":"416-555-5555","Tax Schedule ID":"HST ON/S","Location Code":"TOR","Payment Terms ID":"30 DAYS PD CHEQUE","Credit Limit Amount":"2,000.00","Comment1":"30D PDC /10/11-NSF=WR BK ACCT","Comment2":"","Hold":"1","Price List":"EASTGEN"}]</p>
  
  <p>.</p>
  
  <p>I then use some str_replace to organize the output and make the it look nice. here's what i have:</p>
  
  <p>$newArray = str_replace(":",": ",json_encode($newArray));</p>
  
  <p>$newArray = str_replace("[","[\n",$newArray);</p>
  
  <p>$newArray = str_replace("{","{\n",$newArray);</p>
  
  <p>$newArray = str_replace(",",",\n",$newArray);</p>
  
  <p>$newArray = str_replace("},","\n},",$newArray);</p>
  
  <p>$newArray = nl2br($newArray);</p>
  
  <p>.</p>
  
  <p>the problem i'm having is that the output appears like this:</p>
  
  <p>[</p>
  
  <p>{</p>
  
  <p>"Customer Number": "12345",</p>
  
  <p>"Customer Name": "customer 1",</p>
  
  <p>"Address Code": "PRIMARY",</p>
  
  <p>"Salesperson ID": "aa",</p>
  
  <p>"Ship to Address 1": "12345",</p>
  
  <p>"Ship to Address 2": "123,</p>
  
  <p>fake street",</p>
  
  <p>"Ship to Address 3": "",</p>
  
  <p>"Ship to City": "Toronto",</p>
  
  <p>"Ship to Province": "ONTARIO",</p>
  
  <p>"Ship to Postal Code": "A1A 1A1",</p>
  
  <p>"Country Code": "CA",</p>
  
  <p>"Ship to Country": "CANADA",</p>
  
  <p>"Ship to Contact Person": "John Doe",</p>
  
  <p>"Ship to Phone": "416-555-5555",</p>
  
  <p>"Ship to Fax": "416-555-5552",</p>
  
  <p>"Shipping Method": "OUR TRUCK",</p>
  
  <p>"Bill to Address 1": "12345",</p>
  
  <p>"Bill to Address 2": "123,</p>
  
  <p>fake street",</p>
  
  <p>"Bill to Address 3": "",</p>
  
  <p>"Bill to City": "Toronto",</p>
  
  <p>"Bill to Province": "ONTARIO",</p>
  
  <p>"Bill to Postal Code": "A1A 1A1",</p>
  
  <p>"Bill to Phone": "416-555-5555",</p>
  
  <p>"Tax Schedule ID": "HST ON/S",</p>
  
  <p>"Location Code": "TOR",</p>
  
  <p>"Payment Terms ID": "30 DAYS PD CHEQUE",</p>
  
  <p>"Credit Limit Amount": "2,</p>
  
  <p>000.00",</p>
  
  <p>"Comment1": "30D PDC /10/11-NSF=WR BK ACCT",</p>
  
  <p>"Comment2": "",</p>
  
  <p>"Hold": "1",</p>
  
  <p>"Price List": "EASTGEN"}]</p>
  
  <p>Please notice Ship to Address 2 , Bill to Address 2 , and Credit Limit Amount. because of my str_replace it added a line break there. the problem is that the app i use that reads the json needs it on the same line to register properly. is there a way that i can replace the comma's with a line break UNLESS they are part of an address or another variable?</p>