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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T03:23:23+00:00 2026-05-28T03:23:23+00:00

I’d like to take a string, strip it of all non-alphanumeric characters and convert

  • 0

I’d like to take a string, strip it of all non-alphanumeric characters and convert all spaces into dashes.

  • 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-28T03:23:24+00:00Added an answer on May 28, 2026 at 3:23 am

    I use the following code whenever I want to convert headlines or other strings to URL slugs. It does everything you ask for by using RegEx to convert any string to alphanumeric characters and hyphens.

    function generateSlugFrom($string)
    {
        // Put any language specific filters here, 
        // like, for example, turning the Swedish letter "å" into "a"
    
        // Remove any character that is not alphanumeric, white-space, or a hyphen 
        $string = preg_replace('/[^a-z0-9\s\-]/i', '', $string);
        // Replace all spaces with hyphens
        $string = preg_replace('/\s/', '-', $string);
        // Replace multiple hyphens with a single hyphen
        $string = preg_replace('/\-\-+/', '-', $string);
        // Remove leading and trailing hyphens, and then lowercase the URL
        $string = strtolower(trim($string, '-'));
    
        return $string;
    }
    

    If you are going to use the code for generating URL slugs, then you might want to consider adding a little extra code to cut it after 80 characters or so.

    if (strlen($string) > 80) {
        $string = substr($string, 0, 80);
    
        /**
         * If there is a hyphen reasonably close to the end of the slug,
         * cut the string right before the hyphen.
         */
        if (strpos(substr($string, -20), '-') !== false) {
            $string = substr($string, 0, strrpos($string, '-'));
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text
I want to count how many characters a certain string has in PHP, but
I've got a string that has curly quotes in it. I'd like to replace
I have a text area in my form which accepts all possible characters from
Specifically, suppose I start with the string string =hello \'i am \' me And
this is what i have right now Drawing an RSS feed into the php,
I would like to run a str_replace or preg_replace which looks for certain words

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.