This is what I came across, and his work.
// Always use server validation even if you have JS validation if (!isset($_POST['input']) || empty($_POST['input'])) { // Do stuff } else { $matches = explode(' ',$_POST['input']); $validInput = true; foreach ($matches as $m) { // Check if a slice contains %$[number] as it indicates a sprintf format if (preg_match('/[%\d\$]+/',$m) > 0) { // Match found. Now check if its a valid sprintf format if ($validInput === false || preg_match('/^%(?:\d+\$)?[dfsu]$/u',$m)===0) { // no match found $validInput = false; break; // Invalid sprintf format found. Abort } } } if ($validInput === false) { // Do stuff when input is NOT valid } }
Thanks to Gumbo for the regex pattern that matches both the marker and without it.
Change I realized that the% search is incorrect, since nothing will be checked if it is forgotten / omitted. Above is the new code.
"$ validInput === false ||" may be omitted in the last if-statement, but I have included it for completeness.
source share