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 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.
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
Removing last comma from a foreach loop
Put your values in an array and then implode
that array with a comma (+ a space to be cleaner):
$myArray = array();
foreach ($this->sinonimo as $s){
$myArray[] = '<span>'.ucfirst($s->sinonimo).'</span>';
}
echo implode( ', ', $myArray );
This will put commas inbetween each value, but not at the end. Also in this case the comma will be outside the span, like:
<span>Text1<span>, <span>Text2<span>, <span>Text3<span>
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 to remove the last comma in for loop
<?php
for( $x=1 ; $x<=10 ; $x++){
echo "$x";
if($x!=10)
echo ",";
}
?>
Check for the last iteration and avoid ','
Related Topics
Inserting Google Calendar Entries with Service Account
Mysqli Binding Params Using Call_User_Func_Array
When Do I Have to Declare Session_Start();
Intl Extension PHP_Intl.Dll with Wamp
Can't Insert Chinese Character into MySQL
MySQL Diacritic Insensitive Search (Spanish Accents)
"PHP Fatal Error: Class 'Httprequest' Not Found"
Add Fee Based on Specific Payment Methods in Woocommerce
Output Is in Seconds. Convert to Hh:Mm:Ss Format in PHP
Can PHP's Glob() Be Made to Find Files in a Case Insensitive Manner
PHP Using Preg_Replace:"Delimiter Must Not Be Alphanumeric or Backslash" Error
Getting Actual Value from PHP Simplexml Node
Bind_Param Number of Variables Doesn't Match Number of Parameters in Prepared Statement
Php.Exe' Is Not Recognized as an Internal or External Command, Operable Program or Batch File
$_Files Field 'Tmp_Name' Has No Value on .Jpg File Extension