Problem with foreach for an array in PHP
while($row = mysql_fetch_object($all)) {
$name = $row->name;
$email = $row->email;
$id = $row->id;
$finished_text = '';
$news_content = '';
$buffer = '';
[...]
foreach($text_in_array as $word) {
if($word == '[NAME]'){
$buffer = $name;
}else if($word == '[NAME].'){
$buffer = $name.'.';
}else if($word == '[NAME],'){
$buffer = $name.',';
}else if($word == '[NAME]!'){
$buffer = $name.'!';
}else if($word == '[NAME]"'){
$buffer = $name.'"';
}else if($word == '"[NAME]'){
$buffer = '"'.$name;
}else if($word == '"[NAME]"'){
$buffer = '"'.$name.'"';
}else if($word == '[NAME]."'){
$buffer = $name.'."';
}else if($word == '[NAME],"'){
$buffer = $name.',"';
}else if($word == '[NAME]!"'){
$buffer = $name.'!"';
}else if($word == '*[NAME]*'){
$buffer = '*'.$name.'*';
}else if($word == '**[NAME]**'){
$buffer = '**'.$name.'**';
}else if($word == '[EMAIL]'){
开发者_开发技巧 $buffer = $email;
}else if($word == '[EMAIL].'){
$buffer = $email.'.';
}else if($word == '[EMAIL],'){
$buffer = $email.',';
}else if($word == '[EMAIL]!'){
$buffer = $email.'!';
}else if($word == '[EMAIL]"'){
$buffer = $email.'"';
}else if($word == '"[EMAIL]'){
$buffer = '"'.$email;
}else if($word == '"[EMAIL]"'){
$buffer = '"'.$email.'"';
}else if($word == '[EMAIL]."'){
$buffer = $email.'."';
}else if($word == '[EMAIL],"'){
$buffer = $email.',"';
}else if($word == '[EMAIL]!"'){
$buffer = $email.'!"';
}else if($word == '*[EMAIL]*'){
$buffer = '*'.$email.'*';
}else if($word == '**[EMAIL]**'){
$buffer = '**'.$email.'**';
}else{
$buffer = $word;
}
$news_content .= ' '.$buffer;
}
$finished_text .= Markdown($news_content);
[...]
mail(
'xxxxxxxxxx@gmail.com', //just test later it will be set to $email
$betreff,
$message,
$header
);
}
I don't know why, but sometimes it works perfectly and sometimes it doesn't. Also interesting: Before I created a HTML email with this script, it only worked after I had changed the email address as the last change…
I hope someone can help me. Thanks.
- You should be using a
switch
statement instead of that long string ofif
s. - What's wrong with
$buffer = str_replace(Array("[NAME]","[EMAIL]"),Array($name,$email),$wort);
? - After cleaning that up, if you still get problems, please be more specific on what problems you are having.
精彩评论