The Web Design Group

... Making the Web accessible to all.

Welcome Guest ( Log In | Register )

 
Reply to this topicStart new topic
> PHP Stripslashes Unexpected T-String
Dante Monaldo
post Nov 17 2011, 07:46 PM
Post #1


Advanced Member
****

Group: Members
Posts: 124
Joined: 22-March 09
From: California, USA
Member No.: 8,132



I keep getting slashes "\" in my emails from this PHP contact form. I know to use string stripslashes, etc., but I can't seem to get it right. I keep getting an unexpected T-String error. Maybe I am putting it in the wrong spot?

Here is the code where it should go, somewhere around here...
CODE
    function clean_string($string) {
      $bad = array("content-type","bcc:","to:","cc:","href");
      return str_replace($bad,"",$string);
    }

    $email_message .= "Name: ".clean_string($name)."\n";
    $email_message .= "Email: ".clean_string($email_from)."\n";
    $email_message .= "Telephone: ".clean_string($telephone)."\n";
    $email_message .= "Comments: ".clean_string($comments)."\n";
    


I appreciate the help.
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Christian J
post Nov 17 2011, 08:41 PM
Post #2


.
********

Group: WDG Moderators
Posts: 9,656
Joined: 10-August 06
Member No.: 7



CODE
$bad = array("content-type","bcc:","to:","cc:","href");
return str_replace($bad,"",$string);

You need to loop through the array and use str_replace on its items.
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Dante Monaldo
post Nov 18 2011, 10:35 AM
Post #3


Advanced Member
****

Group: Members
Posts: 124
Joined: 22-March 09
From: California, USA
Member No.: 8,132



Yes, but it's using the function clean_string on each variable. str_replace is a part of the function clean_string.

Or do I need to add backslashes as a part of the $bad variable?

This post has been edited by Dante Monaldo: Nov 18 2011, 10:37 AM
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Christian J
post Nov 18 2011, 01:19 PM
Post #4


.
********

Group: WDG Moderators
Posts: 9,656
Joined: 10-August 06
Member No.: 7



Oops sorry, now I read that "An array may be used to designate multiple needles". Didn't know that was permitted.

I get no errors from this though:
CODE
function clean_string($string)
{
    $bad = array("content-type","bcc:","to:","cc:","href");
    return str_replace($bad,"",$string);
}

$email_message='';

$name="\content-type\"Name\"";
$email_from="to:\"Email\"";
$telephone="\"phone\"";
$comments="<a href=\"http://foo.com/\">link</a>";

$email_message .= "Name: ".clean_string($name)."\n";
$email_message .= "Email: ".clean_string($email_from)."\n";
$email_message .= "Telephone: ".clean_string($telephone)."\n";
$email_message .= "Comments: ".clean_string($comments)."\n";

echo $email_message;

OTOH it shouldn't remove the beginning backslash from $name either (in the other cases, backslashes are just used to escape the double quotes and will go away by themselves).

QUOTE
Or do I need to add backslashes as a part of the $bad variable?

As an extra array item? Might work, but then you may have to escape it with yet another backslash, like this:

CODE
$bad = array("\\","content-type","bcc:","to:","cc:","href");
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Dante Monaldo
post Nov 18 2011, 07:41 PM
Post #5


Advanced Member
****

Group: Members
Posts: 124
Joined: 22-March 09
From: California, USA
Member No.: 8,132



I knew I had the right idea, but I just couldn't get it right.

Thanks!
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
pandy
post Nov 18 2011, 07:58 PM
Post #6


🌟Computer says no🌟
********

Group: WDG Moderators
Posts: 20,730
Joined: 9-August 06
Member No.: 6



A feeling we are all well acquainted with. Well, maybe not Darin. tongue.gif
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post

Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 



- Lo-Fi Version Time is now: 23rd April 2024 - 05:58 AM