Google+

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Verificare string
#1
Incerc sa fac o verificare a unui string sa vad daca acesta contine numai caractere.
Pana acum am folosit functia ctype_alpha(), dar acum am adaugat la string caracterul - (minus), iar functia imi returneaza false.

Cum as putea sa permit totusi folosirea caracterului -, dar sa si verific ca stringul contine numai caractere?
Reply
#2
Răspunsul constă în folosirea expresiilor regulate.

PHP Code:
1
2
3
4
5
6
7
$message = "string-df";
$pattern = '/^[a-zA-Z\-]+$/';
if (preg_match($pattern, $message)) {
    // Match found
} else {
    // No match found.
}

Users : collective term for those who use computers. Users are divided into three types : novice, intermediate and expert.
Novice users : people who are afraid that simply pressing a key might break their computer.
Intermediate users : people who don`t know how to fix their computer after they`ve just pressed a key that broke it.
Expert users : people who break other people`s computers.
Reply
#3
works like a charm Smile multumesc de raspuns Mihu.
Reply
#4
Cu plăcere!
PS : + înseamnă cel puțin un caracter, * înseamnă zero sau mai multe caractere.
Users : collective term for those who use computers. Users are divided into three types : novice, intermediate and expert.
Novice users : people who are afraid that simply pressing a key might break their computer.
Intermediate users : people who don`t know how to fix their computer after they`ve just pressed a key that broke it.
Expert users : people who break other people`s computers.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)