-1

I explored all the previous questions and solutions, nut none of them works in my situation.

I have an input text area where accepts employee ids. End users could use comma , enter, space to separate multiple employee ids, and I need to change any separation into commas.

This is my code:

$emplids = preg_replace('/[,\t\n\r\s]+/',",",$emplids);

And then, I need to only keep the numbers and commas:

$emplids = preg_replace("/[^X0-9,]/", "", $emplids);

And then I need to store them into anarray:

$emplids = explode(",", $emplids);

Now only the end users put comma among employee ids could work, line breaks could not work.

I tried double quotas:

$emplids = preg_replace("/[,\t\n\r\s]+/",",",$emplids);

It doesn't work neither.

It seems like a tiny question, but it really takes me hours. And any hints is highly appreciated!

Falko
  • 17,076
  • 13
  • 60
  • 105

3 Answers3

0

Or you can just get all the ids with the rule that one id can only contain digits, nothing else.

$input = <<<EOF
123 356 353,255
2424,535 35
EOF;

preg_match_all('/(\d+)/',$input, $matches);
print_r($matches[1]);
Karl
  • 833
  • 6
  • 13
0

Putting this here since it'd be ugly in a comment:

php > $x = '\n'; // single quotes
php > $y = "\n"; // double quotes
php > var_dump($x, $y);
string(2) "\n"
string(1) "
"

Note the difference in quoting styles, and the quotes' effect on what PHP sees as being in the string: '-quoted strings do not understand any \ escapes, EXCEPT for \' and \\. Any other escaped char in a '-quoted string is simply a backslash followed by that character.

Marc B
  • 356,200
  • 43
  • 426
  • 500
0

Finally found out the real reason.

I included a script where uses clean function:

if($_POST) {
        $_POST = clean($_POST);
        echo "<pre>" . print_r($_POST,1) . "</pre>";

    }

And in my script, $emplids = $_POST['emplids']; ..... $emplids = explode(",",preg_replace("/[^X0-9,]/", "", preg_replace('/[,\t\n]+/',",",$emplids)));

If remove the clean function, the single ' ' and / within them work well together!