Absolute Newbie, help with strings

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
smithsf22
Forum Newbie
Posts: 2
Joined: Thu Jan 17, 2008 11:36 am

Absolute Newbie, help with strings

Post 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
User avatar
jimthunderbird
Forum Contributor
Posts: 147
Joined: Tue Jul 04, 2006 3:59 am
Location: San Francisco, CA

Re: Absolute Newbie, help with strings

Post 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;
 
 
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: Absolute Newbie, help with strings

Post 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";
smithsf22
Forum Newbie
Posts: 2
Joined: Thu Jan 17, 2008 11:36 am

Re: Absolute Newbie, help with strings

Post by smithsf22 »

Awesome… thanks for the help.
Post Reply