CR/LF introduced into array read?

pillmill
pillmill used Ask the Experts™
on
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?
       $array1=array();
       $acts=array();
       $acts['im'] ="";
       
     $result=mysql_query($str, $link) or die("MySQL Error in reports: ".mysql_error()."  ** Query: ".$str);

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

Open in new window

Comment
Watch Question

Do more with

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

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

Commented:
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
Commented:
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.  
http://php.net/manual/en/function.trim.php

If they are embedded in the string, you can use Example #4 on this page to reduce whitespace.
http://php.net/manual/en/function.preg-replace.php

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