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)
user1038814
  • 9,337
  • 18
  • 65
  • 86
  • Perhaps this is useful: http://stackoverflow.com/questions/1017599 – Marijn van Vliet Jan 31 '12 at 15:58
  • Add `\s` between the brackets in your pattern? That *should* result in your 2nd desired result. – Josh Jan 31 '12 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. – Mark Byers Feb 01 '12 at 08:40

4 Answers4

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.

hakre
  • 193,403
  • 52
  • 435
  • 836
Dmitri Snytkine
  • 1,096
  • 1
  • 8
  • 14
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.

Mark Byers
  • 811,555
  • 193
  • 1,581
  • 1,452
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.

Sabari
  • 6,205
  • 1
  • 27
  • 36
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"
anubhava
  • 761,203
  • 64
  • 569
  • 643