Search code examples
phpregexvalidation

Allow only [a-z][A-Z][0-9] and some symbols in a string


How can I get a string that only contains a to z, A to Z, 0 to 9 and some symbols?

My string is: ��S�o�n�u� �N�i�g�a�m�,� �S�a�i�n�d�h�a�v�i

I would like to eliminate the symbols.


Solution

  • You can test your string (let $str) using preg_match:

    if(preg_match("/^[a-zA-Z0-9]+$/", $str) == 1) {
        // string only contain the a to z , A to Z, 0 to 9
    }
    

    If you need more symbols you can add them before ]