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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T10:39:22+00:00 2026-05-31T10:39:22+00:00

Goodmorning coding lads, I’m writing a small regex too clean filenames from special characters

  • 0

Goodmorning coding lads,

I’m writing a small regex too clean filenames from special characters (&, *, etc.)

This is how my code looks like:

public function clean($string, $bool = false)
{
  $string = html_entity_decode($string, ENT_QUOTES);
  $string = str_replace("'", "", $string);
  $string = str_replace('"', "", $string);
  $string = str_replace("&", "en", $string);
  $string = str_replace("-", "_", $string);

    //ALLE VAGE TEKENS VERVANGEN MET _
    $weirdChars = Proces::normalInput($string , true);

    if(count($weirdChars[0]) > 0)
    {
        foreach($weirdChars[0] as $char)
        {
            $string = str_replace($char, "_", $string);
        }
    }

  if($bool)
    $string = ucfirst(preg_replace('!_+!', '_', strtolower($string)));
  else
    $string = preg_replace('!_+!', '_', strtolower($string));

  if(isset($string[0]) && $string[0] == "_")
    $string = substr($string, 1);
  if(substr($string, -1) == "_")
    return substr($string,0,-1);

  return $string;
}

public function normalInput($string, $bool = false) //STRING
{
  $patern = '/[^_a-zA-Z0-9-]/';
  if(preg_match_all($patern, $string, $matches))
  {
    if($bool)
        return $matches;
    else
    return false;
  }
  else
  {
    if($bool)
        return $matches;
    else
    return true;
  }
}

These 2 methods are working together and working perfectly but I noticed a little problem.
The pattern I use in the normalInput method is like:

$patern = '/[^_a-zA-Z0-9-]/';

This is good but I want to exclude the dots in a filename (otherwise my file-extension will get like blaatfoo_pdf instead of blaatfoo.pdf).

Can you help me with this one?

Kind Regards,

Jordy Suos (take a cup of coffee and a nice sigarette at this beautiful morning.. ON ME)

  • 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-31T10:39:23+00:00Added an answer on May 31, 2026 at 10:39 am

    Goedemorgen. 😉

    You can either use whitelisting or blacklisting:

    // Whitelist alphanumeric, underscore, dash and dot
    $str = preg_replace("/[^a-z0-9-_\.]/", "_", strtolower($str));
    
    // Blacklist
    $str = preg_replace("/[<>'\"\\\/\?\: (... and more) ]/", "_", strtolower($str));
    

    That’s all there’s to it, so you don’t need two functions counting 50 lines. I prefer the whitelist method, because you’ll never know what characters you’re going to receive as input, and there are quite some characters you don’t want in your filenames.

    I’d also suggest to look into your variable and function naming, because $bool isn’t really descriptive. Call it $ucFirst if you want.

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

Sidebar

Related Questions

Goodmorning lads, First of all - R.I.P TPB! Welcome malaysiabay.org for the Dutchies <3
GoodMorning All!!! I am creating subviews of UIViews of small dimensions and adding it
Goodmorning, I'm doing a query to fill a MFC Recordset in my program. Since
Goodmorning everybody, I'm having a little problem with a project for school. We are
the following code #include <iostream> using namespace std; int main(){ char greeting[50] = goodmorning
Good morning all. I'm having a small crisis with table views in a 3.1
Good Morning, I have copied this code from somewere <script type=text/javascript> $(function() { setInterval(rotateImages(),
Goodmorning everybody! I'm not completely new to PHP as in self but still kinda
Test code: string files = C:\Hello; C:\Hi; D:\Goodmorning; D:\Goodafternoon; E:\Goodevening; string[] paths = files.Split(';');
Good morning, I have to use the properties from DeviceNetworkInformation in my application. (http://msdn.microsoft.com/en-us/library/microsoft.phone.net.networkinformation.devicenetworkinformation(v=vs.92).aspx)

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.