Page 1 of 1

Absolute Newbie, help with strings

Posted: Thu Jan 17, 2008 11:41 am
by smithsf22
Hello,
This is my first ever attempt with PHP and I am stuck working with strings. I have this:

Code: Select all

$message .=  "Requested Training: $Area1, $Area2, $Area3, $Area4, $Area5, $Area6, $Area7, $Area8, $Area9, $Area10, $Area11  \n\n";
It of course puts the data correctly but if one of the $Area variables are blank I end up with: “test, test1, , , , , “ As I am sure you know I still display the commas. I figure I can fix this with an IF statement in between each one but I am hoping there is a better way.

Thanks for the help

Re: Absolute Newbie, help with strings

Posted: Thu Jan 17, 2008 12:44 pm
by jimthunderbird

Code: Select all

 
$Area_list = array();
$Area_list[] = $Area1;
$Area_list[] = $Area2;
$Area_list[] = $Area3;
$Area_list[] = $Area4;
$Area_list[] = $Area5;
$Area_list[] = $Area6;
$Area_list[] = $Area7;
$Area_list[] = $Area8;
$Area_list[] = $Area9;
$Area_list[] = $Area10;
$Area_list[] = $Area11;
 
$ouput_list = array();
for($k=0;$k<count($Area_list);$k++){
  if(strlen($Area_list[$k])>0){
     $ouput_list[] = $Area_list[$k];
  }
}
 
$output_list_str = @implode(",",$ouput_list);
 
$message .=  "Requested Training:{$output_list_str}  \n\n";
 
print $message;
 
 

Re: Absolute Newbie, help with strings

Posted: Thu Jan 17, 2008 12:58 pm
by John Cartwright

Code: Select all

$areaList = array($Area1, $Area2, $Area3, $Area4, $Area5, $Area6, $Area7, $Area8, $Area9, $Area, $Area10);
$areaList = implode(', ', array_filter($areaList));
 
$message .=  "Requested Training: $areaList \n\n";

Re: Absolute Newbie, help with strings

Posted: Thu Jan 17, 2008 1:51 pm
by smithsf22
Awesome… thanks for the help.