Remove comma from end of string php
This is a classic question, with two solutions. If you want to remove exactly one comma, which may or may not be there, use: Show
If you want to remove all commas from the end of a line use the simpler:
The rtrim function (and corresponding ltrim for left trim) is very useful as you can specify a range of characters to remove, i.e. to remove commas and trailing whitespace you would write: As with many things in PHP, manipulating strings can be handled a number of ways. I often find myself creating a string of items from an array for presentation on screen or entry into a database. I figured now would be a good time to explore the best method of removing that pesky comma that usually ends up at the end of a looped string (which just so happens to be the wrong way of going about it). In this article, I will show you four different ways of removing the last character of a string and then the best way to do it, which doesn’t involve removing any character but it still likely the reason you are here. Are you trying to get rid of a trailing comma?Most of the time, developers want to loop through items to create a comma-separated list as a string. The quick and dirty method of this is to simply loop through items and add a comma to the end of each. After the loop, you would then need to remove the last comma from the end of your string. If you are ending up with a comma in this manner, you should be taking advantage of PHP’s Here is a clean way to create your comma separated list – no string manipulation needed. $foods = array("banana","orange","apple"); $stringarray = array(); foreach($foods as $fruit) { array_push($stringarray,$fruit); // step #1 - add values to an array } $string = implode("','",$stringarray); // step #2 - implode array echo "SELECT * FROM menu WHERE fruits IN ('$string')"; The output from the above example will be Removing the last character from a string
If you are in a real mess and end up with commas at the beginning AND end of your strings, you can use the regular Both of those functions require that you specify the character(s) to get rid of. Sometimes you don’t know what the last character is and simply want to drop it anyway. In this case, you want to use So to review our options: /* Method 1 */ rtrim("Some random text.", ".") /* Method 2 */ trim("Some random text.", ".") /* Method 3 */ substr("Some random text.", 0, -1) /* Method 4 */ substr_replace("Some random text.", "", -1) Since this is SolidlyStated, I am not going to leave you with 4 options. I am going to leave you with 1. Choose
PHP performance substr vs rtrimFor those of you who were wondering (and I know you are out there),
Here are the results of doing one million iterations on the following two lines: rtrim("this random string of text is short,",","); // takes 1.46 seconds substr("this random string of text is short,",0,-1); // takes 1.42 seconds Substr is faster for large amounts of text as well. How to remove last comma from string PHP?Use the rtrim function: rtrim($my_string, ','); The Second parameter indicates the character to be deleted.. Also make sure you don't have any trailing space after the comma, otherwise this will fail, or do rtrim(trim($my_string), ','). ... . Excellent answer!. How do you remove the comma at the end of a string?To remove the last comma from a string, call the replace() method with the following regular expression /,*$/ as the first parameter and an empty string as the second. The replace method will return a new string with the last comma removed. Copied!
How to remove comma from number in PHP?“remove comma from number format in php” Code Answer's. $var = 18,542.00;. $var = intval(preg_replace('/[^\d. ]/', '', $var));. result :- 18542;. or if you need float.. $var = floatval(preg_replace('/[^\d. ]/', '', $var));. result :- 18542.00;. How do I remove the comma at the end of a cell in Excel?Remove comma from Text String. Select the dataset.. Click the Home tab.. In the Editing group, click on the Find & Replace option.. Click on Replace. This will open the Find and Replace dialog box.. In the 'Find what:' field, enter , (a comma). Leave the 'Replace with:' field empty. ... . Click on Replace All button.. |