Jump to content

Sum two strings


Scummy12

Recommended Posts

Been having a problem getting the sum of multiple strings. For example:

$explode1 = (A number from a webpage)

$explode2= (Another number from a webpage)

these numbers are dynamic and are found using the explode function.

How do I add $explode1 and $explode2 to receive a value.

e.g. if $explode1 = 1000 and $explode2 = 2000

$newvariable would need to equal 3000

Link to comment
Share on other sites

The code I posted should work. Please output the following for me:

var_dump($explode10, $explode11);

 

Also, you have a mix-up of variable names. $bingo11 is not defined.

Yeah just chose part of a very large script, chose the wrong variable. Sorry.

array(2) { [0]=>  string(73) " 1800281000" [1]=>  string(81) "90014050007071046 " } array(2) { [0]=>  string(73) " 1805271000" [1]=>  string(81) "90263550007071046 " } 

That is the output of the var dump

Link to comment
Share on other sites

You realize that there are leading and trailing spaces right? There's an extra space in front of both arrays at index 0 and there is a trailing space of both arrays at index 1.

Yeah I kinda just googled some stuff and put it together, really no idea how to use PHP

Link to comment
Share on other sites

// add this function to the top
// this function takes an array of strings
function f ($array) {
     $new_array = array();
     foreach ($array as $key => $value) {
          $array[$key] = is_array($value)? f($value) : preg_replace('#[^\d]#', '', $value);
     }
     return $new_array;
}

// add these 3 lines after your echos
$explode10 = f($explode10);
$explode11 = f($explode11);

echo $explode[0] + $explode11[0];

 

You can change the function name.

Link to comment
Share on other sites

The leading spaces won't have an effect when the string is converted to an integer for the arithmetic.

 

You're just missing a $.

 

echo $explode10[0]+$explode11[0];

Link to comment
Share on other sites

The leading spaces won't have an effect when the string is converted to an integer for the arithmetic.

 

You're just missing a $.

 

echo $explode10[0]+$explode11[0];

 

Sorry that was actually the php I had, just left out the $. Still outputting 0, any more ideas?

Link to comment
Share on other sites

Something's obviously off when you see this:

[0]=>  string(73) " 1800281000"

 

How could there be 73 characters in that strings. There must be some other characters there. Can you post the updated code?

The two variables I have given you are only a sample of my entire source. For privacy reasons I cannot post the remainder of the file. However each variable should be exactly the same to the two I have posted. Each string will only have 10 numbers maximum.

Link to comment
Share on other sites

That's not what I'm talking about. I'm talking about that the string " 1800281000" has 73 characters. Did you delete some? If not, then it contains some special characters and not just numbers.

 

Can you post the updated code?

...

 

Link to comment
Share on other sites

That's not what I'm talking about. I'm talking about that the string " 1800281000" has 73 characters. Did you delete some? If not, then it contains some special characters and not just numbers.

 

Can you post the updated code?

...

Yes, it contains HTML tags but does not output them. This is the reason?

Link to comment
Share on other sites

*sigh* Please don't make me repeat myself 3 times. Did you try the function I wrote? That should have removed all non-digit characters. Of course, I don't know if that is sufficient. Maybe you just want those specific 10 digits. Either post the updated code that's not working or tell me what happens after you used the function I wrote.

Link to comment
Share on other sites

You really shouldn't be using string functions to access structured XML data, there are specialised tools for that.

 

For example, you could do something like the following (which uses SimpleXML):

 

$urls = array(
    'http://mobsters-fb-apache-dynamic-lb.playdom.com/mob_fb/get_min_hit_list_bounty?user_id=100000952273457&target_id=100000556624340&level=358&session_id=8b56f9b52049dbe329b97c8293c7f78641ad7487&session_key=82573d27b16d6ea8ed2cc819a94c9d52dc93cd71&auth_key=cb07c7575b38df48d82dd53619385faa884db5a2&nocache=1272537280669&',
    'http://mobsters-fb-apache-dynamic-lb.playdom.com/mob_fb/get_min_hit_list_bounty?user_id=100000952273457&target_id=100000556174477&level=358&session_id=8b56f9b52049dbe329b97c8293c7f78641ad7487&session_key=82573d27b16d6ea8ed2cc819a94c9d52dc93cd71&auth_key=cb07c7575b38df48d82dd53619385faa884db5a2&nocache=1272537280669&'
);

// Loop over URLs, put min_cost numbers into array
$counts = array();
foreach ($urls as $url) {
    $counts[] = (int) simplexml_load_file($url)->xml->min_cost;
}

// Find sum
echo array_sum($counts);

Link to comment
Share on other sites

You really shouldn't be using string functions to access structured XML data, there are specialised tools for that.

 

For example, you could do something like the following (which uses SimpleXML):

 

$urls = array(
    'http://mobsters-fb-apache-dynamic-lb.playdom.com/mob_fb/get_min_hit_list_bounty?user_id=100000952273457&target_id=100000556624340&level=358&session_id=8b56f9b52049dbe329b97c8293c7f78641ad7487&session_key=82573d27b16d6ea8ed2cc819a94c9d52dc93cd71&auth_key=cb07c7575b38df48d82dd53619385faa884db5a2&nocache=1272537280669&',



LEGEND!!!!
    'http://mobsters-fb-apache-dynamic-lb.playdom.com/mob_fb/get_min_hit_list_bounty?user_id=100000952273457&target_id=100000556174477&level=358&session_id=8b56f9b52049dbe329b97c8293c7f78641ad7487&session_key=82573d27b16d6ea8ed2cc819a94c9d52dc93cd71&auth_key=cb07c7575b38df48d82dd53619385faa884db5a2&nocache=1272537280669&'
);

// Loop over URLs, put min_cost numbers into array
$counts = array();
foreach ($urls as $url) {
    $counts[] = (int) simplexml_load_file($url)->xml->min_cost;
}

// Find sum
echo array_sum($counts);

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.