I have a form where users will input some ID's. I need them in the database like "123, 124, 125" and so on. What is the best way to use PHP to check and replace the string to fit the correct format? Say the user enters "123,124 , 125", how could I write the script to fix that?

I've tried a bunch of IFs and str_replaces, but that gets stupid cause the user could enter "123. 124, 125"...

EDIT: I forgot to add that the IDs can sometimes contain a decimal... "123.1, 124, 125.2"

Thanks!

    What happens if the user enters "abc, def"?

      It should be invalid. So basically I need to convert it into an array, check to make sure they are all floats, then convert it back into a string in the correct format.

        I would suggest something along these lines:

        <?php
        function formatIdString($input) {
            $input = explode(',', $input);
            $result = array();
            foreach ($input as $value) {
                $value = trim($value);
                if (preg_match('/^\d+(\.\d+)?$/', $value)) {
                    $result[] = $value;
                } else {
                    return false;
                }
            }
            return implode(', ', $result);
        }
        
        if (($str = formatIdString("123,124 , 125")) !== false) {
            echo 'ID: ' . $str;
        } else {
            echo 'Invalid input.';
        }
        
        ?>

        Of course, you would need to tweak the regex pattern to suit your definition of "float".

          Couldn't you use this instead of regular expressions?

          Unfortunately, no. is_float() checks if the variable is a floating point variable, not if what the variable contains can be interpreted as a floating point number.

            Oh, it'd only check the type, not the contents. Very well!

              Sorry about the delays, I've been out sick.

              I'm trying to use preg_replace. It looks like it works just like preg_match, but it replaces the pattern with a specified replacement. Most likely im doing something wrong as I cannot get it to work. Is it possible it's not working because it's not getting a match?

              $data = '123,124. 125';
              
              $check1 = '/ /';
              $replace1 = '';
              
              $check2[0] = '/./';
              $check2[1] = '/,/';
              $check2[2] = '/?/';
              $check2[3] = '/-/';
              $check2[4] = '/:/';
              $check2[5] = '/./';
              $check2[6] = '/$/';
              $check2[7] = '/;/';
              $check2[8] = '/\'/';
              $check2[9] = '/"/';
              
              $replace2[0] = ', ';
              $replace2[1] = ', ';
              $replace2[2] = ', ';
              $replace2[3] = ', ';
              $replace2[4] = ', ';
              $replace2[5] = ', ';
              $replace2[6] = ', ';
              $replace2[7] = ', ';
              $replace2[8] = ', ';
              $replace2[9] = ', ';
              
              
              $d1 = preg_replace($check1, $replace1, $data);
              
              echo = $d1.'<br />';
              
              $d2 = preg_replace($check2, $replace2, $d1);
              
              echo $d2;

              Also, after reviewing what I need the application to do, I realized they will never have decimal points. So I can just make sure they are numbers by using preg_match('/[0-9]{3}/', $data) once I turn it into an array. So now my main concern is cleaning up the data if the user decides to butcher it. 😃

                There's no need to create a separate pattern for each possible character... you could rewrite that all as a single regexp pattern. Moreover, ".", "$", as well as others have special meanings in regular expression syntax and thus must be escaped. You could instead do:

                '/[\.,\?\-:\.$;\'"]/'

                If the user can't enter decimals as you now say, you can simply check the format of their input using preg_match() and then use [man]preg_split/man to get the ID's into an array:

                $data = "  123 ,124' 125";
                if(!preg_match('/^(\s*[0-9]+\s*[\.,\?\-:$;\'"]?)+$/', $data)) {
                	echo "invalid data";
                } else {
                	$id_array = preg_split('/[\s\.,\?\-:$;\'"]+/', $data, -1, PREG_SPLIT_NO_EMPTY);
                
                print_r($id_array);
                }

                I didn't use the fact that the ID numbers must have a width of three - are you positive that this is a concrete condition/restriction? If so, you'll have to modify the preg_match() pattern accordingly.

                  Basically the goal is to replace the invalid character with what is supposed to be (a comma and a space).

                    Write a Reply...