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

  • Home
  • SEARCH
  • 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 9021017
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T05:09:27+00:00 2026-06-16T05:09:27+00:00

Morning SO. I’m trying to determine whether or not a string contains a list

  • 0

Morning SO. I’m trying to determine whether or not a string contains a list of specific characters.

I know i should be using preg_match for this, but my regex knowledge is woeful and i have been unable to glean any information from other posts around this site. Since most of them just want to limit strings to a-z, A-Z and 0-9. But i do want some special characters to be allowed, for example: ! @ £ and others not in the below string.

Characters to be matched on: # $ % ^ & * ( ) + = - [ ] \ ' ; , . / { } | \ " : < > ? ~

private function containsIllegalChars($string)
{
    return preg_match([REGEX_STRING_HERE], $string);
}

I originally wrote the matching in Javascript, which just looped through each letter in the string and then looped through every character in another string until it found a match. Looking back, i can’t believe i even attempted to use such an archaic method. With the advent of json (and a rewrite of the application!), i’m switching the match to php, to return an error message via json.

I was hoping a regex guru could assist with converting the above string to a regex string, but any feedback would be appreciated!

  • 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-16T05:09:28+00:00Added an answer on June 16, 2026 at 5:09 am

    Regexp for a “list of disallowed character” is not mandatory.

    You may have a look at strpbrk. It should do the job you need.

    Here’s an example of usage

    $tests = array(
        "Hello I should be allowed",
        "Aw! I'm not allowed",
        "Geez [another] one",
        "=)",
        "<WH4T4NXSS474K>"
    );
    $illegal = "#$%^&*()+=-[]';,./{}|:<>?~";
    
    foreach ($tests as $test) {
        echo $test;
        echo ' => ';
        echo (false === strpbrk($test, $illegal)) ? 'Allowed' : "Disallowed";
        echo PHP_EOL;
    }
    

    http://codepad.org/yaJJsOpT

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

Sidebar

Related Questions

Morning, I'm trying to split a large text file (15,000,000 rows) using StreamReader/StreamWriter. Is
Morning, I am trying to pass a list of Amazon ASIN's so i can
Morning All, Previously I have been using $(#WebPartWPQ2 .ms-formtable tr:contains('lblName')> td).toggleClass('changedetails'); to customise a
Morning, im trying to store the results of any SProc call, from SQL Server2008,
Morning guys. Using C sharp .net4, and MS Visual Studio 2010. I have Developed
Morning, Im trying to add 1 minute to an existing TimeSpan object. I wanted
Morning, I am trying to create a count of products per category / store
Morning all, I'm fixing errors in a website and escaping a URL using lightbox
morning all, I've been browsing around trying to work out what I've done wrong
Morning y'all! Basically, I'm using a table to store my main data - called

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.