0

I am using a while loop that is as follows:

<?php while(has_sub_field('available_locations')): ?>

    <a href="<?php the_sub_field('location_url'); ?>"><?php the_sub_field('locations'); ?></a>,

<?php endwhile; ?>

The problem is that Last Location has also comma with it. For example:

California, Texas,

But I would like to look it like this:

California, Texas

Since this is containing both HTML and PHP codes, should I store in a variable and use rtrim to cut the last comma, or there is something better solution for it. If I put this in variable, personally I think this is not good solution. Please do respond.

Update: The question is not about only removing last character rather it contains an anchor tag which if I store in a variable, then it is not creating hypertext.Like

Google

But after storing it in variable, the result is like this:

https://www.google.co.in Google

Umar
  • 89
  • 8
  • Ultimatly you either need to know when you're at the end, or assign your results to an array so you can work with a finite set of values. – Scuzzy Jan 16 '18 at 07:44
  • I think this is not a duplicate question. The question is not about only removing last character rather it contains an anchor tag which if I store in a variable, the it is not creating hypertext. – Umar Jan 16 '18 at 08:34
  • Show the HTML source code of the "https://www.google.co.in Google". – ino Jan 16 '18 at 08:50
  • Offert dans la ou les régions suivantes : http://ctfrandev.com/fr/promotions/?regions=491Québec – Umar Jan 16 '18 at 09:25
  • You can be certain that this question is a duplicate on SO. I'm at the cricket so I can't help to search. Start with variable as empty string. Echo it. After first iteration overwrite variable as comma. Such simple tasks are duplicates on SO. – mickmackusa Jan 16 '18 at 10:12
  • https://stackoverflow.com/questions/8312374/echo-a-comma-on-all-but-the-last-value-result-from-mysql-fetch-array @lawrence please close. – mickmackusa Jan 16 '18 at 10:16
  • https://stackoverflow.com/questions/4705814/stripping-last-comma-from-a-foreach-loop – mickmackusa Jan 16 '18 at 12:23

3 Answers3

3

Use PHP substr() function.

Example:

`substr("abcdef", 0, -1);  // returns "abcde"`

So in your case collect all output HTML code to the variable $links and then cut off the last character using substr and print it with echo:

<?php 
$links = '';
while(has_sub_field('available_locations')){ 
   $links .= '<a href="'. the_sub_field('location_url') .'">'.  the_sub_field('locations') .'</a>,';
}
echo substr($links, 0, -1);
?>

Or use build an array and use implode()

<?php 
$links = [];
while (has_sub_field('available_locations')) { 
   $links[] = '<a href="'. the_sub_field('location_url') .'">'.the_sub_field('locations').'</a>';
}
echo implode(', ', $links);
?>

EDIT
Apparently the function used to generate the URL sends its output directly to the browser which cause broken HTML links.
To avoid such situation use PHP Output Control Functions that catch the output and enable the data/string manipulations.

<?php
ob_start(); // Turn on output buffering
while(has_sub_field('available_locations')){
  '<a href="'. the_sub_field('location_url') .'">'.  the_sub_field('locations') .'</a>,';
}
$links = ob_get_contents(); // Get the contents of the output buffer
ob_end_clean(); // Clean (erase) the output buffer and turn off output buffering

echo substr($links, 0, -1); // removal of the last character, the comma.
?>
ino
  • 2,345
  • 1
  • 15
  • 27
2

One option is to collect all your strings in an array, and use implode to join them.

<?php

$links = array();
while( has_sub_field( 'available_locations' ) )
{
  $links[] = '<a href="'.the_sub_field('location_url').'">'.the_sub_field('locations').'</a>';
}
echo implode( ', ', $links );
Scuzzy
  • 12,186
  • 1
  • 46
  • 46
1

I think you are using ACF plugin, to trim the last comma, you can use rtrim() function with get_sub_field() of ACF or merge all links like bellow.

<?php
    $links = array();
    while( has_sub_field( 'available_locations' ) )
    {
      $links[] = '<a href="'.get_sub_field('location_url').'">'.get_sub_field('locations').'</a>';
    }
    echo implode( ', ', $links );
?>
Aiyaz Khorajia
  • 598
  • 2
  • 11