Scummy12 Posted May 2, 2010 Share Posted May 2, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/200420-sum-two-strings/ Share on other sites More sharing options...
Ken2k7 Posted May 2, 2010 Share Posted May 2, 2010 Are you serious? You just add them. $explode1 + $explode2 Quote Link to comment https://forums.phpfreaks.com/topic/200420-sum-two-strings/#findComment-1051761 Share on other sites More sharing options...
Scummy12 Posted May 2, 2010 Author Share Posted May 2, 2010 Yes because I haven't tried that..... Quote Link to comment https://forums.phpfreaks.com/topic/200420-sum-two-strings/#findComment-1051769 Share on other sites More sharing options...
Alex Posted May 2, 2010 Share Posted May 2, 2010 That should work, if it's not working for you it's likely another problem and you need to provide us with relevant code to help us help you. Quote Link to comment https://forums.phpfreaks.com/topic/200420-sum-two-strings/#findComment-1051770 Share on other sites More sharing options...
Scummy12 Posted May 2, 2010 Author Share Posted May 2, 2010 Ok need to add 2 strings containing numerical data, cannot seem to do it. Here is a sample: $bingo9=file_get_contents('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&'); $bingo10=file_get_contents('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&'); $explode10=explode('</min_cost>',$bingo10); $explode11=explode('</min_cost>',$bingo11); echo $explode10[0]; echo $explode11[0]; Need to find the sum of $explode10 and $explode11 Quote Link to comment https://forums.phpfreaks.com/topic/200420-sum-two-strings/#findComment-1051810 Share on other sites More sharing options...
Ken2k7 Posted May 2, 2010 Share Posted May 2, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/200420-sum-two-strings/#findComment-1051811 Share on other sites More sharing options...
Scummy12 Posted May 2, 2010 Author Share Posted May 2, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/200420-sum-two-strings/#findComment-1051812 Share on other sites More sharing options...
Ken2k7 Posted May 2, 2010 Share Posted May 2, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/200420-sum-two-strings/#findComment-1051813 Share on other sites More sharing options...
Scummy12 Posted May 2, 2010 Author Share Posted May 2, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/200420-sum-two-strings/#findComment-1051815 Share on other sites More sharing options...
Ken2k7 Posted May 2, 2010 Share Posted May 2, 2010 // 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. Quote Link to comment https://forums.phpfreaks.com/topic/200420-sum-two-strings/#findComment-1051817 Share on other sites More sharing options...
Scummy12 Posted May 2, 2010 Author Share Posted May 2, 2010 Ok new update, last thing didn't work sorry But now I have echo $explode10[0]+explode11[0]; However it outputs 0. Any thoughts? Quote Link to comment https://forums.phpfreaks.com/topic/200420-sum-two-strings/#findComment-1051903 Share on other sites More sharing options...
Alex Posted May 2, 2010 Share Posted May 2, 2010 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]; Quote Link to comment https://forums.phpfreaks.com/topic/200420-sum-two-strings/#findComment-1051940 Share on other sites More sharing options...
Scummy12 Posted May 2, 2010 Author Share Posted May 2, 2010 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? Quote Link to comment https://forums.phpfreaks.com/topic/200420-sum-two-strings/#findComment-1052076 Share on other sites More sharing options...
Ken2k7 Posted May 2, 2010 Share Posted May 2, 2010 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? Quote Link to comment https://forums.phpfreaks.com/topic/200420-sum-two-strings/#findComment-1052081 Share on other sites More sharing options...
Scummy12 Posted May 2, 2010 Author Share Posted May 2, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/200420-sum-two-strings/#findComment-1052096 Share on other sites More sharing options...
Ken2k7 Posted May 2, 2010 Share Posted May 2, 2010 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? ... Quote Link to comment https://forums.phpfreaks.com/topic/200420-sum-two-strings/#findComment-1052102 Share on other sites More sharing options...
Scummy12 Posted May 3, 2010 Author Share Posted May 3, 2010 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? Quote Link to comment https://forums.phpfreaks.com/topic/200420-sum-two-strings/#findComment-1052218 Share on other sites More sharing options...
Ken2k7 Posted May 3, 2010 Share Posted May 3, 2010 *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. Quote Link to comment https://forums.phpfreaks.com/topic/200420-sum-two-strings/#findComment-1052290 Share on other sites More sharing options...
salathe Posted May 3, 2010 Share Posted May 3, 2010 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); Quote Link to comment https://forums.phpfreaks.com/topic/200420-sum-two-strings/#findComment-1052304 Share on other sites More sharing options...
Scummy12 Posted May 3, 2010 Author Share Posted May 3, 2010 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); Quote Link to comment https://forums.phpfreaks.com/topic/200420-sum-two-strings/#findComment-1052329 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.