CR/LF introduced into array read?

pillmill used Ask the Experts™
Reading from a table into an array and generating a string from the array, I found that
a CR/LF was occasionally introduced into the string. The string is used as a feed to a
graphing program.

How can this be fixed?
       $acts['im'] ="";
     $result=mysql_query($str, $link) or die("MySQL Error in reports: ".mysql_error()."  ** Query: ".$str);

       while($row = mysql_fetch_array($result))
           $acts['im'] .= $row[1]."," ;
$acts['im'] =substr($acts['im'],0,-1);

Open in new window

Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Dave BaldwinFixer of Problems
Most Valuable Expert 2014

There is nothing in the code that you have posted that will do that.

You may want to parse the columns that may contain the CR/LF characters and if found, remove them and update the row with the new value.

It should go something like this:

UPDATE table_name SET field_name=replace(replace(field_name, CHAR(13), ''), CHAR(10), '');

Open in new window

Try it on a test table first to see if you get the results you need.

Also, for the future, look at what's producing the data that goes into the table.  If needed, adjust that before it's inserted.
Most Valuable Expert 2011
Top Expert 2016
This issue sometimes comes up when external input has been processed by a text editor program.  Windows and Mac and Unix all have different line endings and all of the line endings are considered invisible whitespace by the very useful trim() function.  So if the spurious end-of-line characters are on the ends of the string, use trim() to lop them off.

If they are embedded in the string, you can use Example #4 on this page to reduce whitespace.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial