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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T18:52:15+00:00 2026-06-13T18:52:15+00:00

I think I know what the below is doing, but not wanting to make

  • 0

I think I know what the below is doing, but not wanting to make any assumptions wanted to make sure..

RegEx is:

$value = preg_replace(array('/[^a-z0-9_ -]/', '/[ -]+/', '/^-|-$/'), array('', '-', ''), $str);

First part in the array is simple, replaces anything but a-z, 0-9, underscore, space and a hyphen with nothing. Though shouldn’t the hyphen after the space be escaped?

Second part I’m not so sure…. I assume it’s saying replace one or more spaces OR one or more hyphens with a single hyphen? Am I right? But again, escape the hyphen?

Third part is a little confusing….. isn’t the | character in regex the OR operator? So replace a hyphen or a hyphen with nothing? If the | was escaped then I would read it as the whole string would need to match -|- and replace it with nothing?

Lastly….. am I correct in my thinking that it handles the replacements in the position it gets them? So it would handle the first spot in the array first and then move onto the second and third with what’s left of the string; so if the first replacement for example replaces something, then the second replacement will work on the revised version of the string?

  • 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-06-13T18:52:17+00:00Added an answer on June 13, 2026 at 6:52 pm

    After the first replacement, the string will only have letters, numbers, spaces, underscores, and hyphens. The second replacement will turn all consecutive spaces and/or hyphens into a single hyphen, and the third replacement will remove a hyphen from the beginning or the end of the string.

    Other comments:

    • You do not need to escape hyphens in character classes if you put them at the beginning or end of the character class.
    • The third regex /^-|-$/ has the anchors ^ and $, which mean beginning of string and end of string, respectively, so this regex will match a single hyphen but only if it is at the beginning or end of the string.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've been doing some research and I think I know the answer already, but
I think know how to do this in C# but I'm having syntax trouble
I think I know what a B-tree is but what is a B-tree page?
Well I think I know the answer to this, but I would appreciate anybody
I think I already know the answer to this one, but i hope maybe
I think I already know the answer to this but thought I would ask
So I'm not sure if there is a better way to do this, but
I think I know C++ reasonably well and I am thinking about implementing something
for some reason (and I think I know why), when more than one row
Still learning Objective-C / iPhone SDK here. I think I know why this wasn't

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.