Re: trim invisible chars

severin wrote:
> Hi all,
>
> I try "update mybase set name = trim(name)"
>
> to skip spaces.
>
> But 'charriot return'
> (\13\10) are always here.
>
> How can i remove ( or select ) all invisible characteres??

The placement of the "whitespaces" are important when using trim(), it takes
only those that are in the beginning or/and end of the string.

so if you have a string like, "hello\n\rThis is my string", trim will give you
the string "hello\n\rThis is my string" as the charterers in question are in
the middle of the string.

http://www.php.net/manual/en/function.trim.php


If you want to trim characters in the middle of a string, you may want to use
something like eregi_replace().

$newstring=eregi_replace("\n\r",'',"hello\n\rThis is my string");
// $newstring="helloThis is my string"

http://www.php.net/manual/en/function.eregi-replace.php


--

//Aho
Shion [ Di, 16 Januar 2007 12:59 ] [ ID #1597299 ]

Re: trim invisible chars

Thanks J.O. Aho for your answer,

My question is bad written.

Actually, i wonder about the difference between
"trim" : the php function
and "trim" the mysql function.

$mystr = trim( $mystr ); skip all white caracters (space, carriage
return,tab...)

but the sql request:
$sql = "update mybase set name = trim( name ) "
//(here trim is the mysql function)
mysql_query( $sql )

makes the field 'name' of 'mybase' whithout spaces but keeps carriage
return,tab... The mysql trim function only skips spaces.


to skip carriage in mysql i found:
$sql = "update mybase set name = replace( "\n","", name )";

but tab,line feed,NULL and other white caracters are still here...

and i have to do:
$sql = "update mybase set name = replace( "(tab?)","", name )";
$sql = "update mybase set name = replace( "(linefeed?)","", name )";
$sql = "update mybase set name = replace( " ","", name )";

to update my name filed whithout white caracteres.

I'm searching a mysql syntax to do all thouses updates in one only
query. Probably using regexp but i don't know how??

$sql = "update mybase set name = replace( "\n| |\r|\t","", name )";

J.O. Aho wrote:
> severin wrote:
>
>> Hi all,
>>
>> I try "update mybase set name = trim(name)"
>>
>> to skip spaces.
>>
>> But 'charriot return'
>> (\13\10) are always here.
>>
>> How can i remove ( or select ) all invisible characteres??
>
>
> The placement of the "whitespaces" are important when using trim(), it
> takes only those that are in the beginning or/and end of the string.
>
> so if you have a string like, "hello\n\rThis is my string", trim will
> give you the string "hello\n\rThis is my string" as the charterers in
> question are in the middle of the string.
>
> http://www.php.net/manual/en/function.trim.php
>
>
> If you want to trim characters in the middle of a string, you may want
> to use something like eregi_replace().
>
> $newstring=eregi_replace("\n\r",'',"hello\n\rThis is my string");
> // $newstring="helloThis is my string"
>
> http://www.php.net/manual/en/function.eregi-replace.php
>
>
severin [ Di, 16 Januar 2007 13:43 ] [ ID #1597303 ]

Re: trim invisible chars

severin wrote:
> Thanks J.O. Aho for your answer,
>
> My question is bad written.
>
> Actually, i wonder about the difference between
> "trim" : the php function
> and "trim" the mysql function.

OK, then the answer should be different.

> but the sql request:
> $sql = "update mybase set name = trim( name ) "
> //(here trim is the mysql function)
> mysql_query( $sql )
>
> makes the field 'name' of 'mybase' whithout spaces but keeps carriage
> return,tab... The mysql trim function only skips spaces.

The MySQL documentation is few wording about trim
http://dev.mysql.com/doc/refman/5.0/en/string-functions.html

I guess that it don't regard \n and \r as whitespaces


--

//Aho
Shion [ Di, 16 Januar 2007 14:13 ] [ ID #1597304 ]

Re: trim invisible chars

In article <45acc85d$0$297$426a74cc [at] news.free.fr>, severin
<severin.richard [at] free.fr> wrote:

> I'm searching a mysql syntax to do all thouses updates in one only
> query. Probably using regexp but i don't know how??


As far as I am aware, the regexp function in MySQL is only for string
comparison and does not work for substitutions. You will have to manage
this via PHP or string together a query with a ton of replace() to
handle all your replacements.

On the other hand, this is an example of how you can do it in PHP:

$str = "\t\r\ntest\ning\r123\r\n";
echo preg_replace( '/[\r\n \t]+/', "", $str );

--
Koncept <<
"The snake that cannot shed its skin perishes. So do the spirits who are
prevented from changing their opinions; they cease to be a spirit." -Nietzsche
Koncept [ Di, 16 Januar 2007 15:41 ] [ ID #1597308 ]

Re: trim invisible chars

Thanks J.O. Aho for your answer,

My question is bad written.

Actually, i wonder about the difference between
"trim" : the php function
and "trim" the mysql function.

$mystr = trim( $mystr ); skip all white caracters (space, carriage
return,tab...)

but the sql request:
$sql = "update mybase set name = trim( name ) "
//(here trim is the mysql function)
mysql_query( $sql )

makes the field 'name' of 'mybase' whithout spaces but keeps carriage
return,tab... The mysql trim function only skips spaces.


to skip carriage in mysql i found:
$sql = "update mybase set name = replace( "\n","", name )";

but tab,line feed,NULL and other white caracters are still here...

and i have to do:
$sql = "update mybase set name = replace( "(tab?)","", name )";
$sql = "update mybase set name = replace( "(linefeed?)","", name )";
$sql = "update mybase set name = replace( " ","", name )";

to update my name filed whithout white caracteres.

I'm searching a mysql syntax to do all thouses updates in one only
query. Probably using regexp but i don't know how??

$sql = "update mybase set name = replace( "\n| |\r|\t","", name )";

J.O. Aho wrote:
> severin wrote:
>
>> Hi all,
>>
>> I try "update mybase set name = trim(name)"
>>
>> to skip spaces.
>>
>> But 'charriot return'
>> (\13\10) are always here.
>>
>> How can i remove ( or select ) all invisible characteres??
>
>
> The placement of the "whitespaces" are important when using trim(), it
> takes only those that are in the beginning or/and end of the string.
>
> so if you have a string like, "hello\n\rThis is my string", trim will
> give you the string "hello\n\rThis is my string" as the charterers in
> question are in the middle of the string.
>
> http://www.php.net/manual/en/function.trim.php
>
>
> If you want to trim characters in the middle of a string, you may want
> to use something like eregi_replace().
>
> $newstring=eregi_replace("\n\r",'',"hello\n\rThis is my string");
> // $newstring="helloThis is my string"
>
> http://www.php.net/manual/en/function.eregi-replace.php
>
>
severin [ Di, 16 Januar 2007 13:43 ] [ ID #1597376 ]

Re: trim invisible chars

severin wrote:
> Thanks J.O. Aho for your answer,
>
> My question is bad written.
>
> Actually, i wonder about the difference between
> "trim" : the php function
> and "trim" the mysql function.

OK, then the answer should be different.

> but the sql request:
> $sql = "update mybase set name = trim( name ) "
> //(here trim is the mysql function)
> mysql_query( $sql )
>
> makes the field 'name' of 'mybase' whithout spaces but keeps carriage
> return,tab... The mysql trim function only skips spaces.

The MySQL documentation is few wording about trim
http://dev.mysql.com/doc/refman/5.0/en/string-functions.html

I guess that it don't regard \n and \r as whitespaces


--

//Aho
Shion [ Di, 16 Januar 2007 14:13 ] [ ID #1597377 ]

Re: trim invisible chars

In article <45acc85d$0$297$426a74cc [at] news.free.fr>, severin
<severin.richard [at] free.fr> wrote:

> I'm searching a mysql syntax to do all thouses updates in one only
> query. Probably using regexp but i don't know how??


As far as I am aware, the regexp function in MySQL is only for string
comparison and does not work for substitutions. You will have to manage
this via PHP or string together a query with a ton of replace() to
handle all your replacements.

On the other hand, this is an example of how you can do it in PHP:

$str = "\t\r\ntest\ning\r123\r\n";
echo preg_replace( '/[\r\n \t]+/', "", $str );

--
Koncept <<
"The snake that cannot shed its skin perishes. So do the spirits who are
prevented from changing their opinions; they cease to be a spirit." -Nietzsche
Koncept [ Di, 16 Januar 2007 15:41 ] [ ID #1597378 ]
PHP » alt.php » Re: trim invisible chars

Vorheriges Thema: problem:beginner connecting to database with pear
Nächstes Thema: php-oracle variable error