Forum Moderators: coopster

Message Too Old, No Replies

Explode Problem

         

phprockz

3:00 am on Apr 18, 2006 (gmt 0)

10+ Year Member



Hi there,

Iam trying to split words using explode..But for my script last word is not outputing properely.

Example :

Input :singers from db = Venu Madhav, Rajeev Kanakala, Nazar, [comma at last this by default. I need that.]

<?
$names=$row_rscast['singers'];
$names = explode(", ", $names);
$nameSize=sizeof($names);
for($i=0;$i<$nameSize;$i++){
echo "<a href='".$names[$i]."'>".$names[$i]."</a>";
if($i<($nameSize-2)){
echo ", ";
}
}
?>

Output for this script: Venu Madhav, Rajeev KanakalaNazar

This is wrong i need output like

output Needed for me: Venu Madhav, Rajeev Kanakala, Nazar

I couldnt fix it.I hope you guys help me.

Habtom

4:03 am on Apr 18, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The problems is surely here:

if($i<($nameSize-2)){
echo ", ";
}

I think you wanted them to be separated by comma but you didn't want the comma to appear at the end.

Try changing it to:

if($i<($nameSize-1)){
echo ", ";
}

I hope this works for you.

Habtom

phprockz

4:36 am on Apr 18, 2006 (gmt 0)

10+ Year Member



hmm for last word its printing comma[,] at end . I dont want that comma to be printed for last word thats what iam trying.

Output for ur solution : Venu Madhav, Rajeev Kanakala, Nazar,

Needed Output : Venu Madhav, Rajeev Kanakala, Nazar [I dont want comma for last word]

Thanks for your suggestion.

phprockz

7:05 am on Apr 18, 2006 (gmt 0)

10+ Year Member



solved :).This is correct one.

<?
$names="Venu Madhav, Rajeev Kanakala, Nazar, ";
$names = explode(", ", $names);
$nameSize=sizeof($names);
for($i=0;$i<$nameSize;$i++){
echo "<a href='test1.php?cid=$names[$i]'>".$names[$i]."</a>";
if($i<($nameSize-2)){
echo ", ";
}
else
{
echo "";
}
}
?>