Jump to content

detect numeric values make no changes


lovephp
Go to solution Solved by Psycho,

Recommended Posts

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 by lovephp
Link to comment
Share on other sites

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 by ginerjm
  • Like 1
Link to comment
Share on other sites

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 :)

Link to comment
Share on other sites

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

Link to comment
Share on other sites

  • Solution
//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 by Psycho
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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