2

Can you please help me to search the array for decimal numbers or part of it like ". "(dot and space after it) and replace it with zeros or remove the dot?

Below is part of an array:

Array ( [2] => AUD,ADF,1,06-01-2001,3.,3.9532
        [3] => AUD,ADP,1,06-02-2001,99.8222,99.6682
        [4] => AUD,AED,1,06-01-2001,1.8687,1.8664
        [5] => AUD,AFA,1,06-01-2001,2416.57,2413.95
        [6] => AUD,AFN,1,06-01-2001,2416.57,2413.95
        [7] => AUD,ALL,1,06-01-2001,77.,75.9759
        [8] => AUD,AMD,1,06-03-2001,NULL,NULL
        [9] => AUD,ANG,1,06-01-2001,0.9056,0.9046
        [10] => AUD,AOA,1,06-01-2001,3.0751,2.9961
        [11] =>

Please look at [7] there is number like 77. or at [2] there is 3. (there is nothing after the dot).

4 Answers 4

4

Using preg_replace():

$array = array(
    'AUD,ADF,1,06-01-2001,3.,3.9532',
    'AUD,ADP,1,06-02-2001,99.8222,99.6682',
    'AUD,ALL,1,06-01-2001,77.,75.9759',
    'AUD,ALL,1,06-01-2001,78.   ,75.9759'
);
//                         ----RegEx Pattern---  Replace   Input
//                         \/       \/       \/    \/       \/
$new_array = preg_replace('/(\d+)\.(\s*)(?!\d)/', '$1', $array);
//                           ^^^ ^^ ^^^^ ^^^^
//                            |   |   |    | 
// Match any digits [0-9] <---|   |   |    |
//         Match a point  <-------|   |    |
// Match spaces and make it optional<--    |
//                                         |
// Lookahead, which means that if there <--|
// is no digits after:                  <--|
// digit[point][0 or several spaces]    <--|
// The whole thing won't be matched.    <--|

// Addition: the first (\d+) is the first match group
// That's why we used $1 in replace !

// \d -> [0-9]
// \s -> space
// \. -> points have to be escaped
// (?!) -> Lookbehind (check : http://php.net/manual/en/regexp.reference.assertions.php)
// * -> Occurs 0 time or plus
// + -> occurs 1 time or plus
// The '/' at the begin and the end are delimiters

print_r($new_array);

Output:

Array
(
    [0] => AUD,ADF,1,06-01-2001,3,3.9532
    [1] => AUD,ADP,1,06-02-2001,99.8222,99.6682
    [2] => AUD,ALL,1,06-01-2001,77,75.9759
    [3] => AUD,ALL,1,06-01-2001,78,75.9759
)

EDIT: To answer the question that's in the comment below:

Regular Expressions is all about regular patterns. From the desired output, I can see that you want to put everything in quotes except numbers (integer and double/float), we also have dates to put between quotes. So here's a way to do it:

$new_array = preg_replace('/(\d+)\.(\s*)(?!\d)/', '$1', $array); // remove some dots & spaces
$new_array_2 = preg_replace('/([a-zA-Z]+|\d+-\d+-\d+)/', '\'$1\'', $new_array);
//            Match letters <--^-^-^-^-^  ^-^-^-^-^---> Match digits-digits-digits (for the date part), I don't want to use a LOOOONG RegEx to check if it's valid ...

print_r($new_array); // First replacement
print_r($new_array_2); // Second replacement

There is another more reliable way to do this, by using some PHP-Fu:

$new_array = preg_replace('/(\d+)\.(\s*)(?!\d)/', '$1', $array); // remove some dots & spaces
// For this code YOU NEED PHP 5.3+ since it's using anonymous functions
$new_array_2 = array_map(function($val){
    $pieces = explode(',', $val);
    $pieces2 = array_map(function($val2){
        if(preg_match('/^\d+(\.\d+)?$/', $val2)){
            return $val2;
        }else{
            return "'$val2'";
        }
    }, $pieces);
    return(implode(',',$pieces2));
}, $new_array);

print_r($new_array);
print_r($new_array_2);

Output:

Array
(
    [0] => AUD,ADF,1,06-01-2001,3,3.9532
    [1] => AUD,ADP,1,06-02-2001,99.8222,99.6682
    [2] => AUD,ALL,1,06-01-2001,77,75.9759
    [3] => AUD,ALL,1,06-01-2001,78,75.9759
)
Array
(
    [0] => 'AUD','ADF',1,'06-01-2001',3,3.9532
    [1] => 'AUD','ADP',1,'06-02-2001',99.8222,99.6682
    [2] => 'AUD','ALL',1,'06-01-2001',77,75.9759
    [3] => 'AUD','ALL',1,'06-01-2001',78,75.9759
)
Sign up to request clarification or add additional context in comments.

6 Comments

OP asked to remove spaces too.
Thank you, i used the preg_replace and it worked great. can you please explain how did you knew what to write within the function ? or can you direct me to some link that i can learn. (because it worked but i would like to understand how you did it).
Thia is a great - thank you! should i use preg_replace also if i will like to modify the array by adding '' to some of the columns (first, second and fourth)? for example before: Array ( [0] => AUD,ADF,1,06-01-2001,3,3.9532 [1] => AUD,ADP,1,06-02-2001,99.8222,99.6682 ) after: Array ( [0] => 'AUD','ADF',1,'06-01-2001',3,3.9532 [1] => 'AUD','ADP'1,'06-02-2001',99.8222,99.6682 )
I used: $new_array_2 = preg_replace('/([a-zA-Z]+|\d+-\d+-\d+)/', '\'$1\'', $new_array); but there is a situation that the if array will look like : Array ( [2] => AUD,ADF,1,06-01-2001,3.,3.9532 [3] => AUD,ADP,1,06-02-2001,99.8222,99.6682 [4] => AUD,AED,1,06-01-2001,1.8687,1.8664 [5] => AUD,AFA,1,06-01-2001,2416.57,2413.95 and if i use: $new_array_2 = preg_replace('/([a-zA-Z]+|\d+-\d+-\d+)/', '\'$1\'', $new_array); it will also add the ' ' and the NULL value like this 'NULL' when i wanted to do it only for the first,second and the date
i fixed it by removing the a-z from the [a-zA-Z] from the prag_replace and changing the NULL in the array to null (from db side null = NULL)
|
1

You could use intval() to transform your floating points into integers: http://php.net/manual/en/function.intval.php Looks like you might have to split the values (separated by commas), transform them and then join them again...

2 Comments

That's how I understand the question though
I see what you mean my bad. Preg_replace is what you want then
1

Use str_replace, you can pass complete array as input :

$j = array('AUD,ADF,1,06-01-2001,3.,3.9532','AUD,ADP,1,06-02-2001,99.8222,99.6682','AUD,AED,1,06-01-2001,1.8687,1.8664','AUD,AFA,1,06-01-2001,2416.57,2413.95','AUD,AFN,1,06-01-2001,2416.57,2413.95','AUD,ALL,1,06-01-2001,77.,75.9759','AUD,AMD,1,06-03-2001,NULL,NULL','AUD,ANG,1,06-01-2001,0.9056,0.9046','AUD,AOA,1,06-01-2001,3.0751,2.9961');

$j = str_replace(".,", "," , $j);

print_r($j);

Output

Array
 (
[0] => AUD,ADF,1,06-01-2001,3,3.9532
[1] => AUD,ADP,1,06-02-2001,99.8222,99.6682
[2] => AUD,AED,1,06-01-2001,1.8687,1.8664
[3] => AUD,AFA,1,06-01-2001,2416.57,2413.95
[4] => AUD,AFN,1,06-01-2001,2416.57,2413.95
[5] => AUD,ALL,1,06-01-2001,77,75.9759
[6] => AUD,AMD,1,06-03-2001,NULL,NULL
[7] => AUD,ANG,1,06-01-2001,0.9056,0.9046
[8] => AUD,AOA,1,06-01-2001,3.0751,2.9961
)

1 Comment

what about a dot at the end of line?
0

You have to use array_walk() and preg_replace() to do that. Expression for regex should be something like (.)[,\s]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.