lovephp Posted July 19, 2016 Share Posted July 19, 2016 (edited) trying to replace value of strings from array only if its not numeric but if numeric make no changes, the calues are somewhat like this abcd-efgh xyz-abc alpha-lima-lima bravo-charlie-charlie and also like 100-500 1000-5200 100000-800000 now what i wish to achieve is those which are not numeric those stings im trying to remove the - with a space but if its numeric value - should stay intact foreach ($_GET as $key => $value) { $key = str_replace('_', ' ', $key); if(!is_numeric($value)){ $value = str_replace('-', ' ', $value); } echo '<small><b>'.ucstring($key). ':</b> ' .ucstring($value). ',</small>'; } echo '<br/>'; } but doing so the code above making no changes to the numeric values even the numeric values - gets removed Edited July 19, 2016 by lovephp Quote Link to comment https://forums.phpfreaks.com/topic/301514-detect-numeric-values-make-no-changes/ Share on other sites More sharing options...
ginerjm Posted July 19, 2016 Share Posted July 19, 2016 (edited) I don't know which is worse. Your English or your PHP. Did you even read your text prior to hitting the Post button? Ok - you want to modify the non-numeric values only. Got that. And you only want to change the values. So why do you start out changing the key first thing? Using is_numeric is going to fail (tried it) because having that dash in the value automatically makes it NOT numeric. If your pattern is consistently as you stated, you could do a trial replace from _ to a dot which would then be acceptable as a numeric value. THEN you could make your ultimate change from dot to null. PS - you do realize that when the foreach completes, the values in the GET array will not be altered with your current code. Edited July 19, 2016 by ginerjm 1 Quote Link to comment https://forums.phpfreaks.com/topic/301514-detect-numeric-values-make-no-changes/#findComment-1534668 Share on other sites More sharing options...
lovephp Posted July 19, 2016 Author Share Posted July 19, 2016 I don't know which is worse. Your English or your PHP. Did you even read your text prior to hitting the Post button? Ok - you want to modify the non-numeric values only. Got that. And you only want to change the values. So why do you start out changing the key first thing? Using is_numeric is going to fail (tried it) because having that dash in the value automatically makes it NOT numeric. If your pattern is consistently as you stated, you could do a trial replace from _ to a dot which would then be acceptable as a numeric value. THEN you could make your ultimate change from dot to null. PS - you do realize that when the foreach completes, the values in the GET array will not be altered with your current code. well the key values comes as abc_def so i needed to remove the _ from the key, and yes my English is definitely bad but i try hard to improve Quote Link to comment https://forums.phpfreaks.com/topic/301514-detect-numeric-values-make-no-changes/#findComment-1534669 Share on other sites More sharing options...
ginerjm Posted July 19, 2016 Share Posted July 19, 2016 So - English is not your native language. In that case I'm sorry for picking on you but I did not think that was the problem. You actually have pretty good command of the language - I just thought it was American Millennials laziness Quote Link to comment https://forums.phpfreaks.com/topic/301514-detect-numeric-values-make-no-changes/#findComment-1534670 Share on other sites More sharing options...
Solution Psycho Posted July 19, 2016 Solution Share Posted July 19, 2016 (edited) //Create function to be called using array_map() function updateDataArray($value) { //Check if any letters exist if(preg_match("#[a-z]#i", $value)) { //Contains a letter replace "-" with space return str_replace("-", " ", $value); } //Does not contain letter, return unchanged value return $value; } $original_data = array( 'abcd-efgh', 'xyz-abc', 'alpha-lima-lima', 'bravo-charlie-charlie', '100-500', '1000-5200', '100000-800000' ); //Call the function above as a parameter in the array_map function $modified_data = array_map('updateDataArray', $original_data); echo "<b>Before</b><pre>" . print_r($original_data, true) . "</pre>"; echo "<b>After</b><pre>" . print_r($modified_data, true) . "</pre>"; Before Array ( [0] => abcd-efgh [1] => xyz-abc [2] => alpha-lima-lima [3] => bravo-charlie-charlie [4] => B-B-D [5] => 100-500 [6] => 1000-5200 [7] => 100000-800000 ) After Array ( [0] => abcd efgh [1] => xyz abc [2] => alpha lima lima [3] => bravo charlie charlie [4] => B B D [5] => 100-500 [6] => 1000-5200 [7] => 100000-800000 ) Edited July 19, 2016 by Psycho Quote Link to comment https://forums.phpfreaks.com/topic/301514-detect-numeric-values-make-no-changes/#findComment-1534671 Share on other sites More sharing options...
ginerjm Posted July 19, 2016 Share Posted July 19, 2016 And that is how the problem is solved by those who understand regular expressions! Note Psycho mistook your underscores for dashes so correct his code accordingly. Quote Link to comment https://forums.phpfreaks.com/topic/301514-detect-numeric-values-make-no-changes/#findComment-1534672 Share on other sites More sharing options...
Psycho Posted July 19, 2016 Share Posted July 19, 2016 Note Psycho mistook your underscores for dashes so correct his code accordingly. Well, I wrote the code based on his initial post which stated now what i wish to achieve is those which are not numeric those stings im trying to remove the - with a space but if its numeric value - should stay intact But, looking at his last post he magically changed the requirement from dashes to underscores. Quote Link to comment https://forums.phpfreaks.com/topic/301514-detect-numeric-values-make-no-changes/#findComment-1534675 Share on other sites More sharing options...
lovephp Posted July 20, 2016 Author Share Posted July 20, 2016 Well, I wrote the code based on his initial post which stated Thanka lot. No no the key values doea have _ and only the values have the hyphen - But, looking at his last post he magically changed the requirement from dashes to underscores. Quote Link to comment https://forums.phpfreaks.com/topic/301514-detect-numeric-values-make-no-changes/#findComment-1534687 Share on other sites More sharing options...
lovephp Posted July 20, 2016 Author Share Posted July 20, 2016 So - English is not your native language. In that case I'm sorry for picking on you but I did not think that was the problem. You actually have pretty good command of the language - I just thought it was American Millennials laziness no need to say sorry i really don't mind at all. Well i do know how ro read write but where must i put a . Or a commas etc i do not know so this is where i get bad at explaining things Quote Link to comment https://forums.phpfreaks.com/topic/301514-detect-numeric-values-make-no-changes/#findComment-1534688 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.