How to Remove the Last Comma from a String Using PHP

How do I remove the last comma from a string using PHP?

Use the rtrim function:

rtrim($my_string, ',');

The Second parameter indicates the character to be deleted.

How to replace last comma in string with and using php?

To replace only the last occurrence, I think the better way is:

$likes = 'Apple, Samsung, Microsoft';
$likes = substr_replace($likes, ' and', strrpos($likes, ','), 1);

strrpos finds the position of last comma, and substr_replace puts the desired string in that place replacing '1' characters in this case.

How do I remove a comma off the end of a string?

$string = rtrim($string, ',');

Docs for rtrim here

Php how to remove any last commas

rtrim('test,,,,,', ',');

See the manual.

How remove the last comma in my string?

I am guessing that you are trying to take an array of strings of space separated ids and flatten it into a comma separated list of the ids.

If that is correct you can do it as:

$arr = [
'abc def ghi',
'jklm nopq rstu',
'vwxy',
];
$list = implode(', ', explode(' ', implode(' ', $arr)));
echo $list;

output:

abc, def, ghi, jklm, nopq, rstu, vwxy

remove last comma from string in while loop in php?

Take the post titles in an array and Use Implode instead ..

<?php
$cat = get_terms('car_category'); // you can put your custom taxonomy name as place of category.
foreach ($cat as $catVal) {
echo '<b>'.$catVal->name.'</b>';
$postArg = array('post_type'=>'cars','posts_per_page'=>5,'order'=>'desc',
'tax_query' => array(
array(
'taxonomy' => 'car_category',
'field' => 'term_id',
'terms' => $catVal->term_id
)
));

$getPost = new wp_query($postArg);
global $post;

if($getPost->have_posts()){
$str = array();

while ( $getPost->have_posts()):$getPost->the_post();
$str[] = $post->post_title;

endwhile;
}
echo '<div class="yearDiv">';
echo '<span>'.implode(', ', $str).'</span>';
echo '</div>'
}
?>

How can I remove last comma and all spaces from string, then sanitize it using PHP?

Replace spaces and trim commas and spaces at beginning or end:

$result = str_replace(' ', '', trim($string, ', '));

Or:

$result = trim(str_replace(' ', '', $string), ',');

Then if you only want numbers and commas (no letters etc.) maybe:

if(!preg_match('/^[\d,]+$/', $string)) {
//error
}

However this will not error on a single number with no commas.

Remove last comma from string with line breaks

This is what I ended up doing: I just added $Medio = rtrim($Medio,','.PHP_EOL) to remove last line break before removing the last comma as Alex Howansky pointed out.

$Medio=''; //variable to handle the result

$MedioPago='12345'; //variable with numbers given

$chars = str_split($MedioPago); //split the numbers

foreach($chars as $char)
{
if($char=='1')
{
$Medio.='"codigo": "01",'.PHP_EOL;
}
else if($char=='2')
{
$Medio.='"codigo": "02",'.PHP_EOL;
}
else if($char=='3')
{
$Medio.='"codigo": "03",'.PHP_EOL;
}
else if($char=='4')
{
$Medio.='"codigo": "04",'.PHP_EOL;
}
else if($char=='5')
{
$Medio.='"codigo": "05",'.PHP_EOL;
}
}

$Medio = rtrim($Medio,','.PHP_EOL);

Remove any comma before or after a string?

If you just want to remove the first and the last comma, you can use trim

$string = trim($string,",");


Related Topics



Leave a reply



Submit