Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Validate sprintf format from input field with regex

Tags:

validation

php

I have an input field where both regular text and sprintf tags can be entered.

Example: some text here. %1$s done %2$d times

How do I validate the sprintf parts so its not possible them wrong like %$1s ? The text is utf-8 and as far as I know regex only match latin-1 characters.

www.regular-expressions.info does not list /u anywhere, which I think is used to tell that string is unicode.

Is the best way to just search the whole input field string for % or $ and if either found then apply the regex to validate the sprintf parts ?

I think the regex would be: /%\d\$(s|d|u|f)/u

like image 238
Kim Avatar asked Jan 24 '23 22:01

Kim


2 Answers

I originally used Gumbo's regex to parse sprintf directives, but I immediately ran into a problem when trying to parse something like %1.2f. I ended up going back to PHP's sprintf manual and wrote the regex according to its rules. By far I'm not a regex expert, so I'm not sure if this is the cleanest way to write it:

/%(?:\d+\$)?[+-]?(?:[ 0]|'.{1})?-?\d*(?:\.\d+)?[bcdeEufFgGosxX]/
like image 124
NkM Avatar answered Jan 31 '23 07:01

NkM


The UTF-8 modifier is not necessary unless you use UTF-8 in your pattern. And beside that the sprintf format is more complex, try the following

/%(?:\d+\$)?[dfsu]/

This would match both the %s and %1$s format.

But if you want to check every occurrence of % and whether a valid sprintf() format is following, regular expressions would not be a good choice. A sequential parser would be better.

like image 34
Gumbo Avatar answered Jan 31 '23 06:01

Gumbo