Page 1 of 1

trim() doesn't trim...

Posted: Mon Mar 24, 2003 9:17 am
by Erazor2
Hi 2 all,
I wrote a script that gets all fields out of an MySQL table and trims the contents of them.

here is the script:

Code: Select all

<?php
$host=""; $user=""; $pass="";
$vk = mysql_connect($host, $user, $pass) or die ("Verbindung zum MySQL Server konnte nicht hergestellt werden");
       $database="usr_web9_1";
       mysql_select_db($database, $vk) or die ("Fehler bei der DB auswahl");

$sql_query = "SELECT * FROM csc_article ORDER BY id";

$result = mysql_query($sql_query, $vk) or die ("Fehler bei der auswahl!");
        
if($result) {
while($row = mysql_fetch_row($result)) {
       $row[1] = chop($row[1]);
	   $row[2] = chop($row[2]);
	   $row[3] = chop($row[3]);
	   $row[4] = chop($row[4]);
	   $row[5] = chop($row[5]);
	   $row[6] = chop($row[6]);
	   $row[7] = chop($row[7]);
	   $row[8] = chop($row[8]);
	   $row[9] = chop($row[9]);
	   $row[10] = chop($row[10]);
	   $row[11] = chop($row[11]);
	   $row[12] = chop($row[12]);
	   $row[13] = chop($row[13]);
	   $update = "UPDATE csc_article SET category='$row[1]' product_number='$row[2]' desc_short='$row[3]' desc_long='$row[4]' spcial='$row[5]' image_small='$row[6]' image_big='$row[7]' price_a='$row[8]' price_b='$row[9]' price_c='$row[10]' price_d='$row[11]' price_e='$row[12]' tax_rate='$row[13]' WHERE id='$row[0]'";
	   echo "|".$row[1]."|<br>";
       mysql_query($update, $vk);
       }
 }
 else {
 echo "FEHLER<br>".mysql_error($vk)."<br>";
 }
 ?>
What's wrong with that script?
The content of the array is not trimmed at the end....

I hope you can helpt me

Posted: Mon Mar 24, 2003 9:28 am
by twigletmac
What are you trying to trim? Whitespace, new lines, tabs? And what isn't getting removed from where.

Mac

Posted: Mon Mar 24, 2003 10:16 am
by Erazor2
I'm trying to remove whitespaces from the end of each string...
And those whitespaces arenÄt getting removed...

I've tried it with trim() rtrim() and chop() nothing worked :(

Posted: Mon Mar 24, 2003 10:28 am
by volka
maybe those are characters not removed by trim.
try

Code: Select all

<?php
function printHex($s)
{
	for($i=0; $i < strlen($s); $i++)
		printf("%02X ", (ord($s{$i})));
	echo "<br />\n";
}

printHex($row[1]);
?>
to see which characters cause the linebreak.
http://www.php.net/manual/de/function.rtrim.php wrote:Als überflüssige Zeichen werden z.Zt. angesehen: "\n", "\r", "\t", "\v", "\0" und normale Leerzeichen.
  • "\n" = 0A
  • "\r" = 0D
  • "\t" = 09
  • "\v" = 5c 76 :?:
  • "\0" = 00
  • ' ' = 20

Posted: Mon Mar 24, 2003 11:10 am
by Erazor2
I tried your function...

but I get this error:

42 65 6C 6C 65 74 72 69 73 74 69 6B

Fatal error: Cannot redeclare printhex() in /home/www/web9/html/update.php on line 28

and why do I have to know what causes the linebreak??

I just want to remove spaces ;)

Posted: Mon Mar 24, 2003 11:16 am
by volka
you must not put "function printHex($s) { ... }" twice in your file, only once and then

Code: Select all

printHex($row[1]);
printHex($row[2]);
printHex($row[3]);
linebreak, whitespace, whatever ;)
but "42 65 6C 6C 65 74 72 69 73 74 69 6B" is only the word "Belletristik" without whitespaces at all....

Posted: Mon Mar 24, 2003 12:05 pm
by Erazor2
here are some results...
73 69 67 6E 69 65 72 74 65 20 42 FC 63 68 65 72 A0
73 69 67 6E 69 65 72 74 65 20 42 FC 63 68 65 72 A0
73 69 67 6E 69 65 72 74 65 20 42 FC 63 68 65 72 A0
4D E4 72 63 68 65 6E 2D 20 75 6E 64 20 4A 75 67 65 6E 64 62 FC 63 68 65 72 A0
4F 72 69 67 69 6E 61 6C 20 47 72 61 70 68 69 6B 65 6E A0
47 65 73 63 68 69 63 68 74 65 20 75 6E 64 20 50 6F 6C 69 74 69 6B A0
47 65 73 63 68 69 63 68 74 65 20 75 6E 64 20 50 6F 6C 69 74 69 6B A0
42 65 6C 6C 65 74 72 69 73 74 69 6B
I think A0 is \n ?

UPDATE will do the trick

Posted: Mon Mar 24, 2003 5:26 pm
by Adagio
Why don't you just issue a sql query updating all fields trimmed?
If this is more than a one time script, it'll also reduce the resources needed drastically, when all is kept within mysql, and not taken out to php.

There just might be an error in your update part.

I.e.:

UPDATE table SET
field1=TRIM(field1),
field2=TRIM(field2),
field3=TRIM(field3),
field4=TRIM(field4)

Hopefully that'll do the trick.

Posted: Mon Mar 24, 2003 6:12 pm
by m3mn0n
eregi_replace() worked best for me doing a similar job.

Posted: Mon Mar 24, 2003 6:14 pm
by volka
0xA0 is the alternative space character. I've never seen it before and neither will php's nor mysql's trim function delete it. But

Code: Select all

$row[1] = preg_replace("![\r\n\t\v\xa0]+$!", '', $row[1]);
will (works like rtrim except the \0 character)