Я хочу проверить, не пусто ли поле ввода и есть ли у него специальные символы. Я пробовал это:
function stringValidator($field) {
if (!empty($field) && (!filter_var($field, FILTER_SANITIZE_STRING)))
{
return "You typed $field: please don't use special characters
'<' '>' '_' '/' etc.";
} }
PHP даже не пытался это проверить. Какие-нибудь советы?






Звонок preg_match() будет работать нормально.
Код: (Демо)
function stringValidator($field) {
if (!empty($field) && preg_match('~[^a-z\d]~i', $field)) {
return "You typed $field: please don't use special characters '<' '>' '_' '/' etc.";
}
return "valid"; // empty or valid
}
$strings = ["hello", "what_the"];
foreach ($strings as $string) {
echo "$string: " , stringValidator($string) , "\n";
}
Выход:
hello: valid
what_the: You typed what_the: please don't use special characters
'<' '>' '_' '/' etc.
Или звонок ctype_:
Код: (Демо)
function stringValidator($field) {
if (!empty($field) && !ctype_alnum($field)) {
return "You typed $field: please use only alphanumeric characters";
}
return "valid"; // empty or valid
}
$strings = ["hello", "what_the"];
foreach ($strings as $string) {
echo "$string: " , stringValidator($string) , "\n";
}
Выход:
hello: valid
what_the: You typed what_the: please use only alphanumeric characters
@PhilipeDeSouzaSantos Решил ли я вашу проблему? Есть какие-нибудь отзывы для меня?