PHP How to Remove Any Last Commas

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.

Php how to remove any last commas

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

See the manual.

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

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

Docs for rtrim here

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 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

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.

How to remove the last comma from foreach loop?

By placing the HTML in a simple string variable and then using rtrim() on the resulting string before outputting it this should remove the final , from the string

<?php
$out = '';

foreach ($snippet_tags as $tag_data) {
$tags_id = $tag_data->tag_id;
$tagsdata = $this->Constant_model->getDataOneColumn('tags', 'id', $tags_id);
$tag_name=$tagsdata[0]->tag_name;

// move inside loop and amend to place in a simple string var
$out .= '<a href="' . base_url() . 'tags/' . $tag_name . '">' . $tag_name . '</a>,';
?>

echo rtrim($out, ',');

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);

How to remove last comma from output from a for loop

One of the common technique is to add them to an array, then use implode to attach them with glue string ("," for your case). That way there is no need of knowledge when the array is going to end.

<html lang="en">
<head>
<title>Square Numbers</title>
</head>
<body>
<?php
$values = array();
for ($a=1;$a<=10;$a++){
if ($a % 3 != 0){
$values[] = $a * $a;
}
}
echo implode(',', $values);
?>

</body>
</html>


Related Topics



Leave a reply



Submit