Hướng dẫn php comma

Biến chuỗi thành mảng và biến mảng thành chuỗi được ngăn cách bởi dấu phẩy "," [hay comma] trong PHP thì làm như thế nào? [How to transform array to comma separated words string, or string with comma to array?]

Tôi có mảng như thế này:

Array[ [0] => lorem
[1] => ipsum
[2] => dolor
[3] => sit
[4] => amet
]

Yêu cầu sử dụng PHP để biến mảng trên[array] thành chuỗi [string] có dạng như sau:

$string = 'lorem, ipsum, dolor, sit, amet';

Để giải quyết vấn đề này trong PHP ta dùng hàm implode :

$arr = array [ 0 => "lorem", 1 => "ipsum", 2 => "dolor"]; $str = implode [", ", $arr];
 

Ngược lại vấn đề tôi có 1 chuỗi[string], yêu cầu sử dụng PHP để biến chuỗi đó thành mảng [array]

$string = 'lorem, ipsum, dolor, sit, amet';
 

Để giải quyết vấn đề này trong PHP ta dùng hàm explode :

$string = 'lorem, ipsum, dolor, sit, amet'; $arr= explode [", ", $string];

Chúc các bạn thành công! 

In PHP, I have an array of variables that are ALL strings. Some of the values stored are numeric strings with commas.

Nội dung chính

  • How to remove comma from numbers in php?
  • How do you remove the comma at the end of a string?
  • How do I remove a comma from a string in Excel?

What I need:

A way to trim the commas from strings, and ONLY do this for numeric strings. This isn't as straightforward as it looks. The main reason is that the following fails:

$a = "1,435";

if[is_numeric[$a]]
    $a = str_replace[',', '', $a];

This fails because $a = "1435" is numeric. But $a = "1,435" is not numeric. Because some of the strings I get will be regular sentences with commas, I can't run a string replace on every string.

asked Feb 17, 2012 at 20:24

user1082428user1082428

1,0213 gold badges12 silver badges14 bronze badges

1

Do it the other way around:

$a = "1,435";
$b = str_replace[ ',', '', $a ];

if[ is_numeric[ $b ] ] {
    $a = $b;
}

answered Feb 17, 2012 at 20:29

1

The easiest would be:

$var = intval[preg_replace['/[^\d.]/', '', $var]];

or if you need float:

$var = floatval[preg_replace['/[^\d.]/', '', $var]];

answered Apr 7, 2015 at 17:24

JayelaJayela

3451 gold badge4 silver badges7 bronze badges

2

Not tested, but probably something like if[preg_match["/^[0-9,]+$/", $a]] $a = str_replace[...]

answered Feb 17, 2012 at 20:28

KenaniahKenaniah

5,12323 silver badges27 bronze badges

3

It sounds like the ideal solution for what you're looking for is filter_var[]:

$a = filter_var[$a, FILTER_VALIDATE_FLOAT, FILTER_FLAG_ALLOW_THOUSAND];

[Note that it's using FILTER_VALIDATE_FLOAT instead of FILTER_VALIDATE_INT because that one doesn't currently have a FILTER_FLAG_ALLOW_THOUSAND option].

answered Aug 26, 2019 at 22:02

orrdorrd

8,8794 gold badges36 silver badges30 bronze badges

0

Try this .this worked for me

number_format[1235.369,2,'.','']

if you use number_format like this number_format[1235.369,2] answer will be 1,235.37

but if you use like below

number_format[1235.369,2,'.',''] answer will be 1235.37

it's removing the "," of "1,235.37"

Meloman

3,2143 gold badges39 silver badges44 bronze badges

answered Sep 6, 2017 at 6:20

kaushikaushi

1531 silver badge5 bronze badges

1

 function cleanData[$a] {

     if[is_numeric[$a]] {

     $a = preg_replace['/[^0-9,]/s', '', $a];
     }

     return $a;

}

answered Feb 17, 2012 at 20:30

RyanRyan

14.1k8 gold badges60 silver badges102 bronze badges

1

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:

Example:  

$str = "Hey hello, I've got 12,500 kudos for you, spend it well"

function cleannr[$matches]
{
    return str_replace["," , "" , $matches["nrs"]];
}

$str = preg_replace_callback ["/[?P[0-9]+,[0-9]+]/" , "cleannr" , $str];


Output:

"Hey hello, I've got 12500 kudos for you, spend it well"

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

If we'd use /[0-9,]+/ here instead of /[0-9]+,[0-9]+/ the output would be:

"Hey hello I've got 12500 kudos for you spend it well"

answered Sep 6, 2017 at 19:11

FlaxiousFlaxious

1011 silver badge3 bronze badges

How about this:

/**
 * This will parse the money string
 * 
 * For example 1, 234, 456.00 will be converted to 123456.00
 * 
 * @return 
 */
function parseMoney[string $money] : float
{
    $money = preg_replace['/[ ,]+/', '', $money];
    return number_format[[float] $money, 2, '.', ''];
}

Example;

parseMoney['-1, 100,   000.01']; //-1100000.01

answered Jul 16 at 20:16

Emeka MbahEmeka Mbah

15.7k8 gold badges71 silver badges93 bronze badges

How to remove comma from numbers in php?

remove comma in numeric in php.

$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 you remove the comma at the end of a string?

Using the substring[] method We remove the last comma of a string by using the built-in substring[] method with first argument 0 and second argument string. length[]-1 in Java. Slicing starts from index 0 and ends before last index that is string. length[]-1 .

How do I remove a comma from a string 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..

Chủ Đề