2

I would like to replace all special characters in a string except space and -.

For example,

Hello-my näme *is (Jämes93!

to

Hello-my name is James93

or even

Hello-my nme is Jmes93

I have the following but it won't work. Pls can someone help? Thanks

preg_replace('#[^\w-]#',"",$string)
3
  • Perhaps this is useful: stackoverflow.com/questions/1017599 Commented Jan 31, 2012 at 15:58
  • Add \s between the brackets in your pattern? That should result in your 2nd desired result. Commented Jan 31, 2012 at 15:59
  • What specifically do you mean by "it won't work"? Do you get an error message? What is the exact message you get? Do you get an incorrect result? Then what is the result you get? Being specific will help us to help you. Commented Feb 1, 2012 at 8:40

4 Answers 4

5

You don't want to use regex for these. You are much better off using iconv() with transliteration:

$result = iconv("UTF-8", "ASCII//TRANSLIT", $text);

This assumes that original string is encoded in UTF-8 and you want to convert it to ASCII which comes close to your question.

You can convert to/from other charsets the same way just as long as you know the original charset.

By the way, you can't do that with preg_replace, it does not support transliteration.

Sign up to request clarification or add additional context in comments.

Comments

2

Just to answer your specific question, the problem with your code is that preg_replace doesn't actually change the string. You need to assign the result back to $string.

Comments

2

There are several ways to do it:

First method:

$string = "Hello-my näme *is (Jämes93!";

$chars = array(
        'Š'=>'S', 'š'=>'s', 'Đ'=>'D', 'đ'=>'d', 'Ž'=>'Z', 'ž'=>'z', 'Č'=>'C', 'č'=>'c', 'Ć'=>'C', 'ć'=>'c',
        'À'=>'A', 'Á'=>'A', 'Â'=>'A', 'Ã'=>'A', 'Ä'=>'A', 'Å'=>'A', 'Æ'=>'A', 'Ç'=>'C', 'È'=>'E', 'É'=>'E',
        'Ê'=>'E', 'Ë'=>'E', 'Ì'=>'I', 'Í'=>'I', 'Î'=>'I', 'Ï'=>'I', 'Ñ'=>'N', 'Ò'=>'O', 'Ó'=>'O', 'Ô'=>'O',
        'Õ'=>'O', 'Ö'=>'O', 'Ø'=>'O', 'Ù'=>'U', 'Ú'=>'U', 'Û'=>'U', 'Ü'=>'U', 'Ý'=>'Y', 'Þ'=>'B', 'ß'=>'Ss',
        'à'=>'a', 'á'=>'a', 'â'=>'a', 'ã'=>'a', 'ä'=>'a', 'å'=>'a', 'æ'=>'a', 'ç'=>'c', 'è'=>'e', 'é'=>'e',
        'ê'=>'e', 'ë'=>'e', 'ì'=>'i', 'í'=>'i', 'î'=>'i', 'ï'=>'i', 'ð'=>'o', 'ñ'=>'n', 'ò'=>'o', 'ó'=>'o',
        'ô'=>'o', 'õ'=>'o', 'ö'=>'o', 'ø'=>'o', 'ù'=>'u', 'ú'=>'u', 'û'=>'u', 'ý'=>'y', 'ý'=>'y', 'þ'=>'b',
        'ÿ'=>'y', 'Ŕ'=>'R', 'ŕ'=>'r',
    );
echo preg_replace('/[^a-zA-Z0-9 ]/s', '', strtr($string, $chars));

Second method:

echo iconv("UTF-8", "ISO-8859-1//TRANSLIT", $string);

But second method will not replace all the special alphabets. I prefer you to use first one.

Hope this helps you.

Comments

0

You can use code like this:

var_dump ( preg_replace('~[^A-Za-z\d\s-]+~u', '', ('Hello-my näme *is (Jämes93!')) );

OUTPUT:

string(22) "Hello-my nme is Jmes93"

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.