13

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

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
zahir hussain
  • 3,711
  • 10
  • 29
  • 36
  • Please give more details. What is your need? – Karthik May 24 '10 at 11:08
  • 4
    It's a hazy question but there is no need to downvote it into oblivion. There are *really* bad questions around that could use that sort of treatment, but this one is not one of them. – Pekka May 24 '10 at 11:16
  • 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... – zahir hussain May 24 '10 at 11:25
  • use like this, $strchr = "��S�o�n�u� �N�i�g�a�m�,� �S�a�i�n�d�h�a�v�i"; $bodytag = str_replace("�", "", $strchr); – Karthik May 24 '10 at 11:35
  • nice it works... that was already worked... i just restart my mozilla... i got it.. thanks a lot to all... – zahir hussain May 24 '10 at 12:18

7 Answers7

30

You can filter it like:

$text = preg_replace("/[^a-zA-Z0-9]+/", "", $text);

As for some symbols, you should be more specific

Sarfraz
  • 377,238
  • 77
  • 533
  • 578
30

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 ]

Serge S.
  • 4,855
  • 3
  • 42
  • 46
  • You're absolutely right, I should have to surround the pattern in `^` and `$` to prevent match to `'??abs??'` for example, and the whole with `/` delimiters. Sorry & thanks. – Serge S. May 24 '10 at 19:35
12

Don't need regex, you can use the Ctype functions:

In your case use ctype_alnum, example:

if (ctype_alnum($str)) {
    //...
}

Example:

<?php
$strings = array('AbCd1zyZ9', 'foo!#$bar');
foreach ($strings as $testcase) {
    if (ctype_alnum($testcase)) {
        echo 'The string ', $testcase, ' consists of all letters or digits.';
    } else {
        echo 'The string ', $testcase, ' don\'t consists of all letters or digits.';

    }
}

Online example: https://ideone.com/BYN2Gn

Protomen
  • 9,471
  • 9
  • 57
  • 124
1

Both these regexes should do it:

$str = preg_replace('~[^a-z0-9]+~i', '', $str);

Or:

$str = preg_replace('~[^a-zA-Z0-9]+~', '', $str);
Alix Axel
  • 151,645
  • 95
  • 393
  • 500
0

A shortcut will be as below also:

if (preg_match('/^[\w\.]+$/', $str)) {
    echo 'Str is valid and allowed';
} else
    echo 'Str is invalid';

Here:

// string only contain the a to z , A to Z, 0 to 9 and _ (underscore)
\w - matches [a-zA-Z0-9_]+

Hope it helps!

Aditya P Bhatt
  • 21,431
  • 18
  • 85
  • 104
0

If you need to preserve spaces in your string do this

$text = preg_replace("/[^a-zA-Z0-9 ]+/", "", $text);

Please note the way I have added space between 9 and the closing bracket. For example

$name = "!#$John Doe";
echo preg_replace("/[^a-zA-Z0-9 ]+/", "", $name);

the output will be: John Doe

Spaces in the string will be preserved.

If you fail to include the space between 9 and the closing bracket the output will be:

JohnDoe

Hope it helps someone.

Karue Benson Karue
  • 957
  • 12
  • 26
-1

The best and most flexible way to accomplish that is using regular expressions. But I`m not sure how to do that in PHP but this article can help. link

RHaguiuda
  • 3,207
  • 9
  • 37
  • 55