SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Addict
    Join Date
    Feb 2004
    Location
    Rome
    Posts
    296
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    preg_replace question

    Hello

    I have to remove from $list everything between ()

    for example "test(random text)" should return only "test"

    I was using
    $list3 = preg_replace('@\(\w+\)@', '', $list3);

    but it doesn't work if between () there are "-" or other special chars ..

    How to solve the problem ?

    I wish to remove also (a/b\c-te&%st) for example .

    Thank you

  2. #2
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    \w is a shortcut for "word characters" [a-zA-Z0-9_], if you want all chars, use dot in combination with non-greedy flag: .*?

  3. #3
    SitePoint Addict
    Join Date
    Feb 2004
    Location
    Rome
    Posts
    296
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you

    this way ?
    $list3 = preg_replace('@\(\w+.*?\)@', '', $list3);

  4. #4
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well, this reads like this:

    ( , some word chars , nothing or some arbitrary chars , )

    and what you probably need is just

    ( , nothing or some arbitrary chars , )

    i.e.

    @\(.*?\)@

  5. #5
    SitePoint Addict
    Join Date
    Feb 2004
    Location
    Rome
    Posts
    296
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    no if I have

    "some text ( , some word chars , nothing or some arbitrary chars , )"

    I need

    "some text"

    everithing on (..) should be removed , () too.


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
  •