Search code examples
phpregexpreg-split

PHP preg_split - match whitespaces, apostrophes, backticks


I've got following string from INPUT POST textarea:

' 123
'123
'' 123
''123
`123
` 123

I'd like to convert it to the following array:

[0] => 123
[1] => 123
[2] => 123
[3] => 123
[4] => 123
[5] => 123
[6] => 123

I'm trying with this but I don't know how to combine all of them in one regular expression:

\ ('+)('+\s+)(`+)(`+\s+) \

It doesn't seem to work as expected.

Thank you in advance!


Solution

  • Try:

    preg_match_all('/\d+/sim', $text, $result, PREG_PATTERN_ORDER);
    

    and if you want to iterate over matches

    for ($i = 0; $i < count($result[0]); $i++) {
        # your cleaned data = $result[0][$i];
    }