php - Total sum of string made of numbers

252

I have a string which looks like this

12345 45678 25846 44447

Is there some way to make total sum of these numbers in php?

I am trying to make total price from these numbers.

Thank you.

307

Answer

Solution:

Just try with:

$input  = '12345 45678 25846 44447';
$output = array_sum(explode(' ', $input));

Output:

128316
855

Answer

Solution:

You could split the string based on spaces, then do a simple loop like this:

$total=0;
$array=explode(" ",$yourString);
foreach($array as $value)
{
    if (is_numeric($value))
    {
        $total+=$value;
    }
}
echo $total;

People are also looking for solutions to the problem: php - Change text background color for hyper link in PHPExcel

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.