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 7548269
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T09:39:20+00:00 2026-05-30T09:39:20+00:00

I am trying to prepare a string for use as a directory listing. I

  • 0

I am trying to prepare a string for use as a directory listing. I am working with user entered titles and need to remove all but letters and numbers, then replace spaces with _.. I have been able to get somewhat close to this with preg_replace but don’t have hours to learn the ins and outs of Regex at the moment.

Here is an example of the string = This is a -string- that has /characters & other stuff! Let's filter it. What I am shooting for is:
this_is_a_string_that_has_characters_other_stuff_lets_filter_it

I was able to get close with this code, but places where a character was removed the space after it remained. The result was double _’s, which is acceptable but not what I was shooting for.

Any help would be greatly appreciated. Here’s my attempt:

<?php
$string = " This is a -string- that has /characters & other stuff! Let's filter it?";
$cleansedstring = trim($string);
$cleansedstring = strtolower($cleansedstring);
$cleansedstring = preg_replace('/[^ \w]+/', '', $cleansedstring);
$cleansedstring = preg_replace('[\s]', '_', $cleansedstring);
echo $cleansedstring;
?>

UPDATED
Here is what I have figured out from some suggestions on here, seems pretty clean and outputs the string I was shooting for.. suggestions on improvement?

$string = " This is a -string- _that has /characters & other stuff! Let's filter it?23";
$cleansedstring = trim($string);
$cleansedstring = strtolower($cleansedstring);
$cleansedstring = preg_replace('/[^ \pL \pN]/', '', $cleansedstring);
$cleansedstring = preg_replace('[\s+]', '_', $cleansedstring);
echo $cleansedstring;
  • 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-30T09:39:22+00:00Added an answer on May 30, 2026 at 9:39 am

    The regex for removing unwanted characters should not have a + there, and the one that checks for spaces needs a + after it.

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

Sidebar

Related Questions

I'm trying to use SQLBindParameter to prepare my driver for input via SQLPutData .
I am trying to create a table though prepare statement but it is giving
I'm trying to use Command.Prepare with a CommandType.Text query that has a single input
Hello I'm trying to save string into SQLite database, but nothing happens. I have
I am trying to use the TexTMessage interface to prepare a SMS before invoking
I'm trying to use call_user_func_array and mysqli_stmt::bind_param like so: # A. prepare an insert
I'm trying to use the same media player but change the data source. Here
I'm trying to use LsaLogonUser to create an interactive logon session, but it always
I'm trying to implement a Search-Function using c++ and libpqxx. But I've got the
I'm trying to harden some of my PHP code and use mysqli prepared statements

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.