SitePoint Sponsor

User Tag List

Results 1 to 5 of 5

Thread: Regular Expression - Strip "non-keyboard" characters?

  1. #1
    Quake 1 Addict CreedFeed's Avatar
    Join Date
    Feb 2002
    Location
    Milwaukee, WI
    Posts
    294
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Regular Expression - Strip "non-keyboard" characters?

    I am getting data I need to parse and store in a database and within this data are a bunch of non-standard English keyboard characters. What I'd like to do is strip out anything that does not appear on a standard English keyboard, but also keep the copyright symbol, registered symbol, and trademark symbols. Everything else should be stripped away.

    An example of what I'm getting is:

    Code:
    Text®
    In this case, I'd like to run that through preg_replace and return

    Code:
    Text®
    Can anyone help with this?
    -- Steve Caponetto
    Quake 1 Resurrection :: CreedFeed

  2. #2
    Grüße aus'm Pott
    SitePoint Award Recipient Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    2,404
    Mentioned
    39 Post(s)
    Tagged
    1 Thread(s)
    Hi there,

    This should work:

    PHP Code:
    <?php 
    header
    ('Content-Type: text/html; charset=utf-8');
    $String ="Texßßt®";
    echo 
    preg_replace('/[^a-zA-Z®©]/s'''$String);
    It strips out everything except for a-z, A-Z, ® and ©
    How well do you know your JavaScript from your jQuery?
    Check out SitePoint's latest JavaScript challenge


    My blog

  3. #3
    Quake 1 Addict CreedFeed's Avatar
    Join Date
    Feb 2002
    Location
    Milwaukee, WI
    Posts
    294
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    So do I have to just specify the full list of characters I want to include then? I want to keep everything you can type on your standard English keyboard (all of the symbols above the number keys, the brackets, forward/backward slashes, punctuation, new lines, and tabs)?
    -- Steve Caponetto
    Quake 1 Resurrection :: CreedFeed

  4. #4
    Grüße aus'm Pott
    SitePoint Award Recipient Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    2,404
    Mentioned
    39 Post(s)
    Tagged
    1 Thread(s)
    Ah ok.
    Yeah, you basically do have to do that.
    You can of course define character classes as above to make your life easier.
    Also try experimenting with /w which matches any word character.
    How well do you know your JavaScript from your jQuery?
    Check out SitePoint's latest JavaScript challenge


    My blog

  5. #5
    Grüße aus'm Pott
    SitePoint Award Recipient Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    2,404
    Mentioned
    39 Post(s)
    Tagged
    1 Thread(s)
    How well do you know your JavaScript from your jQuery?
    Check out SitePoint's latest JavaScript challenge


    My blog

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •