SitePoint Sponsor

User Tag List

Results 1 to 5 of 5

Hybrid View

  1. #1
    Quake 1 Addict CreedFeed's Avatar
    Join Date
    Feb 2002
    Location
    Milwaukee, WI
    Posts
    296
    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
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,895
    Mentioned
    212 Post(s)
    Tagged
    12 Thread(s)
    Hi there,

    This should work:

    PHP Code:
    <?php 
    header
    ('Content-Type: text/html; charset=utf-8');
    $String ="Text®";
    echo 
    preg_replace('/[^a-zA-Z]/s'''$String);
    It strips out everything except for a-z, A-Z, and

  3. #3
    Quake 1 Addict CreedFeed's Avatar
    Join Date
    Feb 2002
    Location
    Milwaukee, WI
    Posts
    296
    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
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,895
    Mentioned
    212 Post(s)
    Tagged
    12 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.

  5. #5
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,895
    Mentioned
    212 Post(s)
    Tagged
    12 Thread(s)


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
  •