You're almost there already:

$str = "Zebo's [Test]";
echo preg_replace("~['.!,;:@#$%^&*|()?/\\<> \t\r\n\[\]]~", "_", $str);

Output: Zebo_s__Test_

Edited to include [, ], and ' properly - didn't realize you meant that you wanted to replace those.

By the way... You say you want to replace "all special characters," and that your list above is just an "example." You may want to do something broader, like this:

preg_replace("~[^A-Za-z0-9]~", "_", $str);

This would also catch characters like the backtick and other special characters, such as:

`îõ§¶þäô

Answer from elixenide on Stack Overflow
🌐
PHPBuilder
board.phpbuilder.com › d › 10263155-replace-all-special-characters-with-underscore
replace ALL special characters with underscore? - PHPBuilder Forums
December 16, 2003 - I'm writing a file upload script and in order to use ImageMagick successfully, the filename can't have any strange characters like "&" or spaces.. I'm t...
🌐
sebhastian
sebhastian.com › php-replace-special-characters
How to replace special characters in a string with PHP | sebhastian
November 30, 2022 - For example, suppose you want to replace the special character dash (-) with an underscore (_). Here’s how you do it: <?php $str = "learning-php-language"; $new = str_replace("-", "_", $str); print $new; // Output: learning_php_language
🌐
Java2Blog
java2blog.com › home › php › replace space with underscore in php
Replace Space with Underscore in PHP [2 ways] - Java2Blog
December 5, 2022 - Use the str_replace() function to replace with underscore in PHP, for example: str_replace(" " , "_", $str). str_replaces() returns new String with old character space( ) replaced by underscore(_) Use str_replace() method · Output · We used ...
Top answer
1 of 2
2

I would build a regex (character class, to be exact) using your whitelisted characters, and then remove any character which matches the negation of that class.

$allowed_char_array = array("a","b","c","d","e") // and others
$chars = implode("", $allowed_char_array);
$regex = "/[^" . $chars . "]/u";
$input = "imageЙ ййé.png";
echo $regex . "\n";
$output = preg_replace($regex, "_", $input);
echo $input . "\n" . $output;

imageЙ ййé.png
image_ __é.png

If the above be not clear, here is what the actual all to preg_replace would look like:

preg_replace("/[^abcdefghijklmnopqrstuv]/u, "_", $input);

That is, any non whitelisted character would be replaced with just underscore. I did not bother to list out the entire character class, because you already have that in your source code.

Note that the /u flag in the regex is critical here, because your input string is a UTF-8 string. UTF-8 characters may consist of more than one byte, and using preg_replace on them without /u may have unexpected results.

2 of 2
1

You will want to use mb_strtolower() to convert multibyte characters to lowercase safely.

My solution uses strtr() to convert your French accented letters to your preferred form.

Since all characters are lowercased from the onset, you can halve your white list of French characters.

Using pathinfo() helps you to dissect your filename.

Code: (Demo)

$word = 'imageЙ ййé.png';
$parts = pathinfo($word);
$filename = strtr(mb_strtolower($parts['filename']), ['é' =>'é', 'à' => 'à','è' => 'è']);
echo preg_replace('~[^ a-zéàè]~u', '_', $filename) , "." , $parts['extension'];

Output:

image_ __é.png
🌐
Stack Overflow
stackoverflow.com › questions › 70221456 › converting-special-character-to-underscore-in-php
Converting special character to underscore in php - Stack Overflow
Why not use only the characters you do allow. One way to do this is to start by converting UTF-8 to ASCII: ... That should give you a somewhat clean file name. ... Sign up to request clarification or add additional context in comments. ... Find the answer to your question by asking. Ask question ... See similar questions with these tags.
Find elsewhere
🌐
Webmaster World
webmasterworld.com › php › 4007.htm
Replace special chars by underscores - PHP Server Side Scripting forum at WebmasterWorld - WebmasterWorld
My website is written in PHP, and ... news-message to be converted to become a valid url: ... All characters but a to z, A to z and 0 to 9 are replaced by an underscore....
🌐
Devgex
devgex.com › en › article › 00020540
Implementing Space to Underscore Replacement in PHP: Methods and Best Practices - DevGex
November 23, 2025 - The str_replace function provides PHP developers with a simple yet powerful tool for handling character replacement needs in strings. In the specific application scenario of space-to-underscore replacement, this function demonstrates excellent performance and ease of use.
🌐
PHP
php.net › manual › en › function.preg-replace.php
PHP: preg_replace - Manual
To ensure that there will be no regular characters at the end of a word, just convert all regular characters to their final forms, then run this function. Enjoy! ... From what I can see, the problem is, that if you go straight and substitute all 'A's wit 'T's you can't tell for sure which 'T's to substitute with 'A's afterwards. This can be for instance solved by simply replacing all 'A's by another character (for instance '_' or whatever you like), then replacing all 'T's by 'A's, and then replacing all '_'s (or whatever character you chose) by 'A's: <?php $dna = "AGTCTGCCCTAG"; echo str_repl
🌐
Grabthiscode
grabthiscode.com › php › php-replace-space-with-underscore
Php replace space with underscore - code example - GrabThisCode.com
php convert spaces to underscores · php replace \n by <br> remove empty spaces php · php split string along spaces · replace all numbers in string php · remove spaces from string php · replace all php · remove space from string php · php replace space with 20 · replace multiple characters one string php ·
🌐
Basantmallick
basantmallick.com › freelance website developer in delhi ncr india › php › how to replace all special characters except underscore and period in php?
how to replace all special characters except underscore and period in php?
February 20, 2021 - <?php $string="a quick borown fox jump over the lazy dog & ( jkl/)-ov"; echo $string = preg_replace('/[^a-zA-Z0-9_.]/', '-', strtolower($string)); ?>