Jump to content

trim(), ltrim() and rtrim() not working


ebpowell

Recommended Posts

I am trying to write a database (MySQl) clean-up script that ltrims all of the values in a certain field.

But, ltrim leaves the whitespace on the returned string (read from MySQL character fields).

 

trim exhibits the same behavior.

 

Code:

 

$arrFields = array('FNAME', 'LNAME', 'ADDRESS'. 'EMail', 'Phone');

foreach ($arrFields as $strField)

{

$sql = 'SELECT MemberID, '.$strField.' FROM Members';

//poll the database for the values

$arrData = getData($sql);

foreach ($arrData as $arrRow)

{

$intMemberID = $arrRow['MemberID'];

$strData = (string) $arrRow[$strField];

 

if ($strField != 'Phone')

{

$strValue =ltrim($strData);

var_dump($strValue);

}

 

/Code

 

Here is an example of my output:

 

string(4) "Chip" UPDATE Members SET FNAME="Chip" WHERE MemberID = 71

string(6) " Sandy" UPDATE Members SET FNAME=" Sandy" WHERE MemberID = 276

string(5) "Steve" UPDATE Members SET FNAME="Steve" WHERE MemberID = 119

string(7) " Robbie" UPDATE Members SET FNAME=" Robbie" WHERE MemberID = 271

 

 

Note the spaces prepending Sandy and Robbie.

 

Any guesses? I am mystified.

 

I have tried specifying the characters in the ltrim function as well (e.g ltrim($strData, " \t") etc)

 

Thanks,

 

Eric

 

 

Link to comment
https://forums.phpfreaks.com/topic/47177-trim-ltrim-and-rtrim-not-working/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.