andreas
andreas

Reputation: 8014

Replace selected characters in PHP string

I know this question has been asked several times for sure, but I have my problems with regular expressions... So here is the (simple) thing I want to do in PHP:

I want to make a function which replaces unwanted characters of strings. Accepted characters should be: a-z A-Z 0-9 _ - + ( ) { } # äöü ÄÖÜ space

I want all other characters to change to a "_". Here is some sample code, but I don't know what to fill in for the ?????:

<?php

// sample strings
$string1 = 'abd92 s_öse';
$string2 = 'ab! sd$ls_o';

// Replace unwanted chars in string by _
$string1 = preg_replace(?????, '_', $string1);
$string2 = preg_replace(?????, '_', $string2);

?>

Output should be: $string1: abd92 s_öse (the same) $string2: ab_ sd_ls_o

I was able to make it work for a-z, 0-9 but it would be nice to allow those additional characters, especially äöü. Thanks for your input!

Upvotes: 1

Views: 1454

Answers (2)

agent-j
agent-j

Reputation: 27953

Replace [^a-zA-Z0-9_\-+(){}#äöüÄÖÜ ] with _.

$string1 = preg_replace('/[^a-zA-Z0-9_\-+(){}#äöüÄÖÜ ]/', '_', $string1);

This replaces any characters except the ones after ^ in the [character set]

Edit: escaped the - dash.

Upvotes: 1

Justin Morgan
Justin Morgan

Reputation: 30700

To allow only the exact characters you described:

 $str = preg_replace("/[^a-zA-Z0-9_+(){}#äöüÄÖÜ -]/", "_", $str);

To allow all whitespace, not just the (space) character:

 $str = preg_replace("/[^a-zA-Z0-9_+(){}#äöüÄÖÜ\s-]/", "_", $str);

To allow letters from different alphabets -- not just the specific ones you mentioned, but also things like Russian and Greek, or other types of accent marks:

 $str = preg_replace("/[^\w+(){}#\s-]/", "_", $str);

If I were you, I'd go with the last one. Not only is it shorter and easier to read, but it's less restrictive, and there's no particular advantage to blocking stuff like и if äöüÄÖÜ are all fine.

Upvotes: 2

Related Questions