Jump to content

[SOLVED] preg / str_replace


Schlo_50

Recommended Posts

Hi guys,

 

I have 6 variables which need to be placed into a string, each separated by commas. I can do that, BUT, sometimes not all of the variables will have a value so when I print my string I get something like the following:

 

var1, var2, , var4, var5, var6

 

I have tried to use str_replace to get a nice comma separated list without a comma at the end of the last number but it's not really working well. Has anybody got any ideas as to how I can code this please? My example code follows:

 

$relevant = "1a";
$relevant_two = "2b";
$relevant_three = "3c";
$relevant_four = "";
$relevant_five = "5e";
$relevant_six = "6f";

$strt_string = "$relevanta, $relevantb, $relevantc, $relevantd, $relevante, $relevantf";

$srt_string = str_replace(' ,', '', $strt_string);

 

Thanks in advance!

Link to comment
https://forums.phpfreaks.com/topic/108514-solved-preg-str_replace/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.