|
Re: Escaped Characters in string output [message #81543 is a reply to message #81536] |
Tue, 25 September 2012 03:35  |
Heinz Stege
Messages: 189 Registered: January 2003
|
Senior Member |
|
|
On Mon, 24 Sep 2012 20:13:06 -0700 (PDT), M. Katz wrote:
> To prepare string data for a database, I'd like to escape all of the special characters. Other languages have built-in functions for this. Does IDL, or someone from the community have a routine they trust?
>
> The special characters are
>
> \ ' " NUL (ASCII 0) \n and \r and CTRL-Z
>
> In essence I'd like to create a function that searches for these and places a backslash in front of each occurrence. See mysql_real_escape_string() http://dev.mysql.com/doc/refman/5.0/en/mysql-real-escape-str ing.html in PHP. In principle I suppose this is a good task for Regular Expressions, but I'm not enough of an expert.
>
> Thanks
I don't know about any built-in routine within IDL. I would make use
of a conversion table. This may be more efficient than regular
expressions:
table=[bytarr(1,256),bindgen(1,256)]
table[0,[byte('\''"'),10b,13b,26b]]=(byte('\'))[0]
input='"abc" 123 \xyz ''aaa''a'
output=table[*,byte(input)]
output=string(output[where(output ne 0b)])
print,input,output,form='(a)'
Cheers, Heinz
|
|
|