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

Get Up Genie

Get Up Genie Logo Get Up Genie Logo

Get Up Genie Navigation

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

Mobile menu

Close
Ask A Question
  • Terms of Use
  • Terms of Use
  • Privacy Policy
  • Privacy Policy
  • Cookie Policy
  • Cookie Policy
Home/ Questions/Q 14482
In Process

Get Up Genie Latest Questions

todaytech
  • 0
  • 0
todaytech
Asked: December 13, 20232023-12-13T01:58:57+00:00 2023-12-13T01:58:57+00:00In: Programmers

How to replace space with hyphen or dash for image filename on upload wordpress PHP Code?

  • 0
  • 0
How to replace space with hyphen or dash for image filename on upload wordpress PHP Code?
how can i remove empty spaces form a file name when uploading to the serverreplace space with hyphen for image filename on uploadreplace the file – file name adding dashes
1
  • 1 1 Answer
  • 69 Views
  • 0 Followers
  • 0
    • Report
  • Share
    Share
    • Share on Facebook
    • Share on Twitter
    • Share on LinkedIn
    • Share on WhatsApp

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. todaytech
    todaytech
    2023-12-13T01:59:14+00:00Added an answer on December 13, 2023 at 1:59 am
    This answer was edited.

    How to remove accents from uploaded filenames
    For this WordPress had a built-in function we can use: remove_accents

    This function replaces characters like À Â Å with A and € with E for example.


    How to remove accents from uploaded filenames
    For this WordPress had a built-in function we can use: remove_accents

    This function replaces characters like À Â Å with A and € with E for example.

    How to remove unwanted characters from uploaded filenames
    To achieve this we we replace all character in the filename with nothing, except for the characters we want to allow. In this case we want to preserve A-Z a-z 0-9 – _ . and space characters. This can be achieved by using PHP function preg_replace which performs a search and replace on a string based on a regular expression.
    // Remove every character except A-Z a-z 0-9 . - _ and spaces.
    $sanitized_filename = preg_replace( '/[^A-Za-z0-9-_\. ]/', '', $filename );

    How to replace spaces in a filename with underscores (or dashes)
    Here we also use preg_replace to replace whitespace with an underscore. Note that multiple spaces are replaced by a single underscore.
    // Replace spaces (blanks) with an underscore.
    $sanitized_filename = preg_replace( '/[[:blank:]]+/', '_', $filename );

    If you prefer dashes over underscores use:

    // Replace spaces (blanks) with a dash.
    $sanitized_filename = preg_replace( '/[[:blank:]]+/', '-', $filename );

    Putting it together
    You can copy / paste the following code snippet in your WordPress theme’s functions.php. For every file uploaded in WordPress or Gravity Forms upload fields the filename will be cleaned and converted.
    /**
    * Convert and clean uploaded filenames in WordPress.
    * - Remove chars with accents etc, also replaces € with E.
    * - Remove every character except A-Z a-z 0-9 . - _ and spaces.
    * - Replace spaces (blanks) with an underscore.
    *
    * @author gravitywp.com
    * @param string $filename
    * @return string
    */
    add_filter( 'sanitize_file_name', 'gwp_sanitize_file_name', 10, 1 );
    function gwp_sanitize_file_name( $filename ) {

    // Remove chars with accents etc, also replaces € with E.
    $sanitized_filename = remove_accents( $filename );

    // Remove every character except A-Z a-z 0-9 . - _ and spaces.
    $sanitized_filename = preg_replace( '/[^A-Za-z0-9-_\.[:blank:]]/', '', $sanitized_filename );

    // Replace spaces (blanks) with an underscore.
    $sanitized_filename = preg_replace( '/[[:blank:]]+/', '_', $sanitized_filename );

    return $sanitized_filename;
    }

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

Sidebar

Ask A Question

Stats

  • Questions 11k
  • Answers 2k
  • Best Answers 30
  • Users 11k
  • Popular
  • Answers
  • Shinchan

    https://www.devex.com/people/can-i-cancel-and-get-a-refund-with-british-airways-2458295

    • 29 Answers
  • juunu

    https://www.giantbomb.com/forums/general-discussion-30/speakrealperson-how-does-southwest-24-hour-check-i-1926113/

    • 14 Answers
  • nishirajvanshi5

    Where to find the activation code?

    • 9 Answers
  • jacov72332
    jacov72332 added an answer Houston is a hub for various industries, including oil and… June 19, 2025 at 1:37 pm
  • matebiz60
    matebiz60 added an answer A great CRM program should make client management easier, automate… June 19, 2025 at 12:41 pm
  • jcrcarremovals
    jcrcarremovals added an answer Yes, at Japanese Car Removals, we accept cars in any… June 19, 2025 at 12:28 pm

Related Questions

  • I don't have money to buy new computer, can I ...

    • 1 Answer
  • I'm stupid, is there any learning coding for dummy around?

    • 1 Answer
  • Why do ASP-NET developers prefer Angular, not ReactJS/Vue.js?

    • 0 Answers
  • What are the best programming in India ?

    • 0 Answers
  • How to approach applying for a job at a company ...

    • 7 Answers

Top Members

getadmin

getadmin

  • 3k Questions
  • 3k Points
Enlightened
Digital

Digital

  • 2k Questions
  • 2k Points
Enlightened
janvi4sure

janvi4sure

  • 636 Questions
  • 788 Points
Enlightened

Trending Categories

Blogs Business Construction Cryptocurrency Digital Marketing Education Electric Vehicle Entertainment Events Fashion Health Law Lifestyles News Others Real Estate SEO Services Software Technology Travel

Explore

  • Home
  • Add group
  • Groups page
  • User Profile
  • Feed
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags

Footer

Get Up Genie

Get up Genie helps to share the latest free best question and answer websites across the world. We help to share the trending questions and answers in all industries people.

About Us

  • Meet The Team
  • Home
  • Blog
  • About Us
  • Contact Us

Legal Stuff

  • Terms of Use
  • Terms of Use
  • Privacy Policy
  • Privacy Policy
  • Cookie Policy
  • Cookie Policy

Help

  • Knowledge Base
  • Knowledge Base
  • Support
  • Support

Follow

© 2024 Get Up Genie. All Rights Reserved
With Love by Get Up Genie

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.