SitePoint Sponsor

User Tag List

Results 1 to 13 of 13
  1. #1
    SitePoint Member
    Join Date
    Jun 2009
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Help matching all symbols except defined

    I need to match all symbols except letters, numbers, "_", " ", "-", "&"? I tried the following but it is not working:
    PHP Code:
    preg_match_all '/[^a-z0-9_ -&]/i' $string $matches ); 

  2. #2
    play of mind Ernie1's Avatar
    Join Date
    Sep 2005
    Posts
    1,252
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    $string "abcd_ -&";

    preg_match_all '/[^a-z0-9_ \-&]/i' $string $matches );

    print_r($matches); 
    my mobile portal
    ghiris.ro

  3. #3
    SitePoint Member
    Join Date
    Jun 2009
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    grrr i forgot '-' is special symbol inside [] and i have to escape it
    thanks

  4. #4
    ¬.¬ shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    Can get away with just this: /[^-\w &]/
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  5. #5
    SitePoint Member
    Join Date
    Jun 2009
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    "-" not escaped?

  6. #6
    ¬.¬ shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Torrodon View Post
    "-" not escaped?
    When it is in the front like that, you don't need to.
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  7. #7
    SitePoint Member
    Join Date
    Jun 2009
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ahh didn't know that
    thanks a lot

  8. #8
    @php.net Salathe's Avatar
    Join Date
    Dec 2004
    Location
    Edinburgh
    Posts
    1,397
    Mentioned
    65 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by logic_earth View Post
    Can get away with just this: /[^-\w &]/
    Bear in mind that \w may match more than just [a-zA-Z0-9_] depending on the locale in use. For example, with a French locale (fr_FR) some accented characters also fall into \w. So the string "abcdé_ -&" would report no matches (see accented é). Of course, the OP may want accented letters to be allowed?
    Salathe
    Software Developer and PHP Manual Author.

  9. #9
    SitePoint Member
    Join Date
    Jun 2009
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i see
    so \w won't do the trick
    i'll stick with
    PHP Code:
    preg_match_all '/[^a-z0-9_ \-&]/i' $string $matches ); 
    10x @Salathe


    edit:
    '/[a-z]/i' don't match foreign chars, right? or should i do '/[abcdef...ABCDEF.....0123....]/' to be sure?

  10. #10
    @php.net Salathe's Avatar
    Join Date
    Dec 2004
    Location
    Edinburgh
    Posts
    1,397
    Mentioned
    65 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Torrodon View Post
    '/[a-z]/i' don't match foreign chars, right? or should i do '/[abcdef...ABCDEF.....0123....]/' to be sure?
    No, the ranges a-z, A-Z and 0-9 will not match accented characters.
    Salathe
    Software Developer and PHP Manual Author.

  11. #11
    SitePoint Member
    Join Date
    Jun 2009
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    so
    PHP Code:
    '/[a-z]/i' == '/[a-zA-Z]/' 
    right?

  12. #12
    @php.net Salathe's Avatar
    Join Date
    Dec 2004
    Location
    Edinburgh
    Posts
    1,397
    Mentioned
    65 Post(s)
    Tagged
    0 Thread(s)
    Yes, that's right.
    Salathe
    Software Developer and PHP Manual Author.

  13. #13
    SitePoint Member
    Join Date
    Jun 2009
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thank you very much @Salathe


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
  •