Getting rid of Carriage return when processing MySQL Data in PHP
I have a field in a database that has a carriage return. It is an address (although it could be anything) and it is the following two lines:
8923 Chowsnik Dr.
If you take the two lines above, you will also take the empty line that it has below the address as well. This is what is in the database.
Now, in php, I need to eliminate this extra carriage return white space. I have tried trim, str_replace(\n\r, etc...
But, nothing seems to work. I am using the return along with a jquery flexigrid control and this is what is not liking that extra carriage return.
So, is there anything I can do to get rid of this dead space at all?
copy and paste this:
$new_string= preg_replace("/[^a-z0-9 ]/i", '', $string);
hielo
>> jquery flexigrid
Ahh, most likely you are feeding it a json string. Try:
$data = preg_replace('/[\r\n]/U','',$field);
swhitlow
ASKER
Nope. that didn't work either. Also, note the details above. It is php that is creating the javascript. So, it is returning a json post formatted string. Don't know if that matters or not, but I thought I would mention it.
try escaping the back slashes:
$data = preg_replace('/[\\r\\n]/U','',$field);
swhitlow
ASKER
nope. didn't work either. Not sure if this will help but, here is the data I am getting back from Firebug:
rows: [
{id:'aa505f50-dade-6272-2717-47f2b333320e',cell:['aa505f50-dade-6272-2717-47f2b333320e', 'Add to Route','2 Big Consolidation Corp 418993','(754
) 301-4377','9 IBM Path','St. Petersburg','CA','37000']},
{id:'d89e5bd7-20d4-8831-65dc-47f2b3d721b2',cell:['d89e5bd7-20d4-8831-65dc-47f2b3d721b2', 'Add to Route','2 Tall Stores 376377','(236) 854-7906','8923 Chessie
Dr.
','Indianapolis','IN','46217']},
{id:'c0c5e69c-59fa-f341-1aa8-47f2b3a9b06c',cell:['c0c5e69c-59fa-f341-1aa8-47f2b3a9b06c', 'Add to Route','A.G. Parr PLC 828674','(743) 194-6946','345 Sugar
Blvd.','St. Petersburg','CA','39570']},
Notice the extra carriage return on the line that says "8923 Chessie Dr." then it drops down one line and has the "Indianapolis".
That is where the problem is. If I go into the database and get rid of that one line, It solves the problem. But, I obviously have to code for the user who might input the one line in there. Even if by accident.
hielo
can you post the php portion that is querying the db and encoding the data?
nope. didn't work either. I can't believe that this is giving me this much trouble! :(
hielo
If you echo the address immediately after you trim() it, does the output still reflect the newline character?Could something else be appending it later on in the code?
swhitlow
ASKER
just so everyone knows here - I had to do a json_encode($value) and change the code above so that instead of looping through the records and adding the values to concatenate the string, it looped through the values and added the string to an array. I then used json_encode to properly encode it and it now works perfectly!
Thanks everyone for their suggestions!
Not sure what to do about the points now....what is the appropriate thing to do here?
str_replace(\r, etc
str_replace(\n, etc
separately.
In unix/linux there is just a /n
in mac os there is just /r
win windows there is /r/n
your replace had /n/r