How do I remove a trailing "comma" from my PHP for each function?

I have a working piece of PHP code which iterates a number of comma-separated values.
What I am having a difficult time doing is removing the trailing comma at the end of the last item found.

<?php foreach($vendor_cat as $vendorcat) { ?>
    <span id="vendor_category_list"><a href="<?php echo $vendorcat['href']; ?>" ><?php echo $vendorcat['category_name']; ?></a>,</span>
<?php } ?>

Open in new window

any help would be greatly apprecaited.

thanks in advance
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Dave BaldwinFixer of ProblemsCommented:
Your first problem is that ,</span> is not part of a PHP string but part of the HTML content which PHP doesn't even see.
badwolfffAuthor Commented:

thanks. Could the function not be rewritten to count the number of items for that loop and add that many -1 commas?

I've done similar stuff in JavaScript but in PHP I am not sure how to do it.

Dave BaldwinFixer of ProblemsCommented:
PHP doesn't touch the HTML that is in between the PHP tags.  You have to put all of that in a PHP string for PHP to do anything with it.  If you do that then you can replace the final instance with a version that does not have the comma.
Exploring SQL Server 2016: Fundamentals

Learn the fundamentals of Microsoft SQL Server, a relational database management system that stores and retrieves data when requested by other software applications.

badwolfffAuthor Commented:
Could you help me with the code?
badwolfffAuthor Commented:
In the meantime I was able to get an iteration count inside the for each function using:
<?php echo count($vendor_cat); ?>
Dave BaldwinFixer of ProblemsCommented:
That doesn't help as long as the data is not in a PHP string.  This is untested but it is the basic idea.  You create the whole string in a PHP variable and then replace the last part.
$thestring = '';
foreach($vendor_cat as $vendorcat) { 
	$thestring .= '<span id="vendor_category_list"><a href="'.$vendorcat['href'].'" >'.$vendorcat['category_name'].'</a>,</span>'
$thestring = substr_replace($thestring, '</span>', -8, 8) //

Open in new window

Ray PaseurCommented:
In case you're new to PHP and want to learn the language, this article has some helpful links.
badwolfffAuthor Commented:
Thanks Ray, trying to fix someone else's code. Mostly I can manage myself but sometimes there are these awkward problems.

Dave by the way after your function the out is empty now.
Dave BaldwinFixer of ProblemsCommented:
I forgot to add...
echo $thestring;

Open in new window

at the very end.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.