WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Remove last comma. Simple quest. Please help (5 posts)

  1. web20donkey
    Member
    Posted 2 years ago #

    Hi all. I am a newbie to wordpress. I am trying to remove the last comma from this syntax:

    <li data-tags="<?php if($terms) { foreach ($terms as $term) { echo $term->name . ','; } } ?>">

    This output something like:

    <li data-tags="Logo Design,Print Design,">

    And I don't want the last comma.

    A little help would be great.

    Thank you.

  2. keesiemeijer
    moderator
    Posted 2 years ago #

    Try it with something like this (not tested):

    <?php if($terms) {
    $the_terms = '';
    foreach ($terms as $term) {
    $the_terms .= $term->name .',';
    }
    echo '<li data-tags="'.trim($the_terms, ',') .'">';
    }
    ?>
  3. Digital Raindrops
    Member
    Posted 2 years ago #

    Untested:

    <?php if($terms) {
    	$str = "";
    	foreach ($terms as $term) {
    		$str = $str .$term->name . ',';
    	}
    	?>
    	<li data-tags="<?php echo substr($str,0,($len-1)); ?>">
    <?php } ?>

    Edit:
    How was that two replies!

    HTH

    David

  4. web20donkey
    Member
    Posted 2 years ago #

    Thank you very much for your prompt replies, both syntaxes work like a charm.

  5. keesiemeijer
    moderator
    Posted 2 years ago #

    Glad you got it resolved.

    @Digital Raindrops
    great minds think alike, those two code examples are nearly identical! (even the untested bit)

Topic Closed

This topic has been closed to new replies.

About this Topic