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

  • SEARCH
  • Home
  • 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 7660617
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T13:29:25+00:00 2026-05-31T13:29:25+00:00

I have a PHP variable, say $myvariable = te xt!@ na#@)(me+=&t^*ext?>;.’na^%me; I want to

  • 0

I have a PHP variable, say

$myvariable = "te    xt!@ na#@)(me+=&t^*ext?>;.'na^%me";

I want to replace special characters and group of special characters including blank space with a single underscore _. The string may contain & and it may be replaced with and.

The result of previous variable should be;

te_xt_na_me_andt_ext_na_me

How can I do this in PHP?

  • 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-31T13:29:25+00:00Added an answer on May 31, 2026 at 1:29 pm

    This assumes, anything but “characters” is regarded disposable.

    $patterns = array(
        '/&/'             => 'and',  // Ampersand to "and"
        '/[^[:alpha:]]+/' => '_'     // Anything *but* a character to underscore
    );
    
    $result = preg_replace(array_keys($patterns), array_values($patterns), $input);
    

    The last pattern replaces groups of one or more occurences of “non-word” characters according to the current locale1 (and thus including white-space).


    1 Side-note (might be irrelevant): if the server the script runs on has en_US as locale, the following replacements occur:

    $input = 'app!le___s &!   orän=%ges';
    $result = 'app_le_s_and_or_n_ges';
    

    If the locale is de_DE, this would be the result:

    $result = 'app_le_s_and_orän_ges';
    

    Because ä is part of [[:alpha:]] in this particular locale. The obvious solution to circumvent this would be to substitute the character class for [a-zA-Z].

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

Sidebar

Related Questions

I have the following PHP code to remove special characters from a variable; <?php
i have value in php variable like that $var='2.500000550'; echo $var what i want
i have a variable $cartTotal in cart.php page i want to use that same
Say I have a variable containing PHP code, can I include its content as
Say I have a file (php as it happens), with a number of variable
Lets say we have 3 files: File1.php, File2.php, File3.php I want to have it
I have a PHP-variable called $go_Adress which contains the adress I need to get
I would like to have the $ (dollar sign) which indicates a php variable
I have a PHP function that takes a variable number of arguments (using func_num_args()
I have 2 php files. I am unable to get B's global variable from

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.