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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T00:40:33+00:00 2026-06-06T00:40:33+00:00

I need to filter out strings that contain anything other than letters, numbers, dots,

  • 0

I need to filter out strings that contain anything other than letters, numbers, dots, hyphens, apostrophes or spaces.

Strings with letters, numbers, dots, hyphens and spaces pass the test, but when they contain an apostrophe it fails. (The escape characters on the dot and hyphen don’t seem to be making any difference so I put them in just to be sure)

Any thoughts on this?

if (preg_match("/[^a-zA-Z0-9\.\-\'\\s]/", $some_var)){
    echo "Invalid characters";
}
  • 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-06T00:40:34+00:00Added an answer on June 6, 2026 at 12:40 am

    Your answer doesn’t compile due to the escaping of some of the characters (escaped as being part of a string, before passed into the preg_match function).

    You could try double escaping:

    if (preg_match("/[^a-zA-Z0-9\\.\\-\\'\s]/", $some_var)){ 
        echo "Invalid characters"; 
    } 
    

    but escaping dot, hyphen and apostrophe is not needed, so you can simplify:

    if (preg_match("/[^a-zA-Z0-9.'\s-]/", $some_var)){ 
        echo "Invalid characters"; 
    } 
    

    Note that the hyphen is moved to the end, so that it’s not mistaken for a character range. Personally, I prefer to escape it to prevent other developers accidentally adding new characters to the end of the list and causing unexpected behaviour.

    So:

    if (preg_match("/[^a-zA-Z0-9.\\-'\s]/", $some_var)){ 
        echo "Invalid characters"; 
    } 
    

    Finally, you may want to check your input (the $some_var variable) – does this actually contain a backslash as PHP sometimes will add this in (eg. user types “can’t”, but gets sent through as “can\’t” – you may need to stripslashes first).

    Eg.

    if (preg_match("/[^a-zA-Z0-9.\\-'\s]/", stripslashes($some_var))){ 
        echo "Invalid characters"; 
    } 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an NSArray and need to filter out any strings that are null
I have a two dimensional ArrayList that I need to filter the duplicates out
I need to have a REGEX that would filter out and only accept a
I need to filter out junk data in SQL (SQL Server 2008) table. I
I have some text i need to filter out a list of bad words
I have list of catalog paths and need to filter out some of them.
I need to return a list of counties, but I need to filter out
Using the following example, I need to filter out the line containing 'ABC' only,
I need to filter out some values before they are bound from POST data
I need to filter out anchor tags in a string. For instance, Check out

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.