Need a hand to decipher some code

Hi Guys.

Could anyone help me decipher this piece of code from my contact page.

What i’m not quit sure about i how this piece of code works [php] return str_replace($bad,"",$string);[/php] As i understand it $bad is going to be replaced by replace in $string. But it dont understand what it does when there is nothing in replace?

Can anyone explain it?

Here is how the codes looks like, in case you need to see it in a context:

[php]
$email_message = “Form details below.\n\n”;

function clean_string($string) {
$bad = array(“content-type”,“bcc:”,“to:”,“cc:”,“href”);
return str_replace($bad,"",$string);
}

$email_message .= "First Name: ".clean_string($first_name)."\n";
$email_message .= "Last Name: ".clean_string($last_name)."\n";
$email_message .= "Email: ".clean_string($email_from)."\n";
$email_message .= "Telephone: ".clean_string($telephone)."\n";
$email_message .= "Comments: ".clean_string($comments)."\n";

[/php]

Basically, the line takes $string, checks if any element from the array $bad is in it, and if yes, then it replaces it with “” - which means it simply removes that string out of the main string.

So if for example the email address comes in as "to:[email protected]", then the string “to:” is found in the array $bad as something that needs to be replaced, so it gets replaced with nothing, resulting in "[email protected]".

str_replace allows you to use an array to look for matches (in this case $bad).

I hope this makes more sense now.

Here’s the official PHP manual for it - I have that page open in my browser all the time…
http://us.php.net/manual/en/function.str-replace.php

Sponsor our Newsletter | Privacy Policy | Terms of Service