Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6323915
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T16:36:35+00:00 2026-05-24T16:36:35+00:00

I know this question has been asked several times for sure, but I have

  • 0

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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T16:36:35+00:00Added an answer on May 24, 2026 at 4:36 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know this question has been asked several times, but I can't quite seem
I know that this question have been asked several times. But I can't get
I know this question has been asked several times on SO, but none of
I know this question has been asked several times, but I would like to
I know this question has been asked a few other times but I have
I know this question has been asked many times, but here are my specific
I know that this question has been asked several times and I've read all
I know this question has been asked a zillion times, but I still feel
I know this question has been asked before numorous times but they seem to
I know this question has been asked countless of times. But none of them

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.