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
PHP Rename Array Keys in Multidimensional Array
How to Check for a Specific Type of Object in PHP
Wordpress Plugin -> Call to Undefined Function Wp_Get_Current_User()
How to Take an Array, Divide It by Two and Create Two Lists
Get "Content-Type" Header of Request in PHP
How to Send a Bytearray (From Flash) and Some Form Data to PHP
How to Use Curl and PHP Simple HTML Dom Parser with Object
PHP Authentication with Multiple Domains and Subdomains
Returning Header as Array Using Curl
How to Employ "If Exists" for Creating or Dropping an Index in MySQL
Can a User Alter the Value of $_Session in PHP
Using Array_Intersect on a Multi-Dimensional Array
Href="" Automatically Adds to Current Page Url (In PHP). Can't Figure It Out