Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

addslashes

Status
Not open for further replies.

axman505

Technical User
Jun 20, 2001
489
0
0
US
Does anyone know if there is an equivelent to the addslashes php function floating around out there on the net somwhere for perl?
 
If addslashes does what I think it does (not a PHP guru), yes: q and qq.

For example, if I want to assign this line of HTML to a var in Perl:

<form name="myform" method="GET" action="/wibble/foo.cgi">

I would obviously not want to say
Code:
$formline = "<form name....>";
because I would then have to backslash all those quotes.

q and qq to the rescue!

Use them like this:
Code:
$formline = q{<form name....>};
$formline = qq{<form name....>};
Use qq all the times you would normally use double-quotes - it allows variable interpolation within the string; use q whenever you would use single-quotes. The delimiter around the string is completely arbitrary, just like in Perl regular expressions; i chose curly-braces because there weren't any inside the string I was quoting.

There are other similar operators. perldocs.org is down for rehosting, but I found this link to a Perl Cookbook page on the subject:


Cheers!

--G
 
Well, what i am doing is parsing an XML files, and then inserting the contents into mysql. The program is some fields contain weird charatchers like: ",',*. etc which gum up the works when you try to insert it.

What addslashes does is that it will escape all characters that need to be escaped.
 
There's quotemeta, which quotes all non-"word" chars.
If that's more than you need, how about something like
Code:
$string =~ s/(["'*])/\\$1/g;
That'll backslash any of ",', or *. You can modify the char class to suit.
 
Yes, as a PHP guru, a s/ / /g will serve you just as well as addslashes. You just have to make sure that you identify all of them.....it shoudl tell you all of the characters it will slash at
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top