12/30/2023 0 Comments Php foreach remove last comma"Hey hello I've got 12500 kudos for you spend it well" If we'd use /+/ here instead of /+,+/ the output would be: In this case the pattern (regex) differs from the one given in the accepted answer since we don't want to remove the other commas (punctuation). "Hey hello, I've got 12500 kudos for you, spend it $str = "Hey hello, I've got 12,500 kudos for you, spend it well" function cleannr($matches) If you want to remove commas from numbers inside a string that also contains words, the easiest way I think would be to use preg_replace_callback: Like if(preg_match("/^+$/", $a)) $a = str_replace(.)ġ4.1k 8 gold badges 60 silver badges 102 bronze badges Or if you need float: $var = floatval(preg_replace('//', '', $var)) ģ55 1 gold badge 4 silver badges 7 bronze badges The easiest would be: $var = intval(preg_replace('//', '', $var)) Because some of the strings I get will be regular sentences with commas, I can't run a string replace on every string.ĭo it the other way around: $a = "1,435" ģ2.5k 20 gold badges 88 silver badges 102 bronze badges This fails because $a = "1435" is numeric. The main reason is that the following fails: $a = "1,435" This isn't as straightforward as it looks. Some of the values stored are numeric strings with commas.Ī way to trim the commas from strings, and ONLY do this for numeric strings. In PHP, I have an array of variables that are ALL strings.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |