1
//Chars to be replaced      
$suchmuster = array();
$suchmuster[0] = 'á';
$suchmuster[1] = 'à';
$suchmuster[2] = 'â';

$suchmuster[3] = 'é';
$suchmuster[4] = 'è';
$suchmuster[5] = 'ê';

$suchmuster[6] = 'í';
$suchmuster[7] = 'ì';
$suchmuster[8] = 'î';

$suchmuster[9] = 'ó';
$suchmuster[10] = 'ò';
$suchmuster[11] = '/ô/';

$suchmuster[12] = 'ú';
$suchmuster[13] = 'ù';
$suchmuster[14] = 'û';

$suchmuster[15] = ' ';
$suchmuster[16] = '.';
$suchmuster[17] = ',';
$suchmuster[18] = '-';
$suchmuster[19] = '_';

//Replaces

$ersetzungen = array();
$ersetzungen[0] = 'a';
$ersetzungen[1] = 'a';
$ersetzungen[2] = 'a';

$ersetzungen[3] = 'e';
$ersetzungen[4] = 'e';
$ersetzungen[5] = 'e';

$ersetzungen[6] = 'i';
$ersetzungen[7] = 'i';
$ersetzungen[8] = 'i';

$ersetzungen[9] = 'o';
$ersetzungen[10] = 'o';
$ersetzungen[11] = 'o';

$ersetzungen[12] = 'u';
$ersetzungen[13] = 'u';
$ersetzungen[14] = 'u';

$ersetzungen[15] = '';
$ersetzungen[16] = '';
$ersetzungen[17] = '';
$ersetzungen[18] = '';
$ersetzungen[19] = '';

$newmessage = preg_replace($suchmuster, $ersetzungen, strtolower($message));

The above code was created to replace some special chars with normal ones but no matter which $nachricht (= $message) I put in, it always gives back an empty string!

Thanks for your help.

EDIT

I Changed it to:

//Chars to be replaced

$suchmuster[0] = '/ /';
$suchmuster[1] = '/./';
$suchmuster[2] = '/,/';
$suchmuster[3] = '/-/';
$suchmuster[4] = '/_/';

//Replaces

$ersetzungen[0] = '';
$ersetzungen[1] = '';
$ersetzungen[2] = '';
$ersetzungen[3] = '';
$ersetzungen[4] = '';

$newmessage = iconv('UTF-8', 'ISO-8859-1//TRANSLIT//IGNORE', strtolower($message));

$newmessage = preg_replace($suchmuster, $ersetzungen, $newmessage);

It still gives back an empty string.

5
  • 2
    Enable error_reporting. Except /ô/ none of your $suchmusters are valid regexps. If the charset is Latin-1, consider str_replace or strtr instead. Commented Dec 7, 2013 at 13:40
  • You know you can assign an array like this: $suchmuster = array('á', 'à', 'â', /* etc. */); Commented Dec 7, 2013 at 13:42
  • If you want just get rid of the diacritics, you can also use iconv like iconv('UTF-8', 'US-ASCII//TRANSLIT', $string). Commented Dec 7, 2013 at 13:44
  • Thank you. error_reporting doesn't return anything. It doesn't work even if I put every char to be replaced in "/ /" Commented Dec 7, 2013 at 13:44
  • Marc Korpel, thank you I found this a few seconds ago. I also don't want any special chars like "-" or "_" for example. Commented Dec 7, 2013 at 13:45

3 Answers 3

1

Instead of using several patterns, you can also combine the pattern to:

$nachrichtneu = preg_replace('/[,.\s_-]/', '', $nachrichtneu);

And strtolower will not convert Á and the like to á. First do the iconv conversion, then the strtolower.

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

4 Comments

@NicoEnking And the reason you got back an empty string is explained by Kaii in their answer.
@HamZa Isn't escaping the dot necessary to prevent it matching every character?
@MarcelKorpel In a character class, the dot loses it's meaning, so it becomes a literal dot characters Which means it's redundant to escape it. See this demo. The same goes for a hyphen at the begin or end of a character class. See this demo.
@HamZa I just tested this myself, too. Thanks for the links to regex101.com, I didn't know that site and it looks very useful.
1

Regarding your updated question:

Regular expressions use special control characters. In your case it is /./ which causes your result to be empty. . means "any character" in regex, so you are replacing each and every character with nothing.

use preg_quote() to escape special characters in regular expressions. Example: $xy = '/' . preg_quote('.') . '/';

But you can do this much simpler without using regular expressions, like:

$replaces = array(
    '.' => '',
    ',' => '',
    // ...
);
$message = str_replace(array_keys($replaces), array_values($replaces), $message);

Comments

0

change your code through

$suchmuster = array();
$suchmuster[0] = "/á/";
$suchmuster[1] = "/à/";

...

1 Comment

I have updated my code but it still doesnt work (See my edit above)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.