#1
  1. Classical Computing
    Devshed Novice (500 - 999 posts)

    Join Date
    May 2008
    Location
    Melbourne, Australia
    Posts
    893
    Rep Power
    102

    Regex symbol for a tab


    I love the idea of this new forum.

    Today I tried using a PHP regex to validate basic HTML code in a textarea. I tested the regex by posting some valid HTML code but the regex kept rejecting it. I finally realized that there were some tabs in the HTML that were causing the problem. I'd like to keep the option of having tabs in the code.

    So my question is, what's the best way to allow for a tab in a regex? I tried just typing a tab in the regex, and it worked, but it seems a poor way to go about it. I tried entering this tab symbol--& # 0 9 ; (without spaces, of course)--but that didn't work. Is there a better one, or is this just not Kosher anyway?

    I'd love to know if anyone has any suggestions. I'm a newbie at this, and still using eregi, since I'm not confident about preg_match yet. If anyone knows of a really clear outline of the preg_match syntax I'd be grateful.
    Last edited by Arem; August 20th, 2008 at 08:05 AM.
    Simple is beautiful.
  2. #2
  3. No Profile Picture
    I haz teh codez!
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Dec 2003
    Posts
    2,549
    Rep Power
    2337
    \t is usually the way to do it.
  4. #3
  5. kill 9, $$;
    Devshed Supreme Being (6500+ posts)

    Join Date
    Sep 2001
    Location
    Shanghai, An tSín
    Posts
    6,897
    Rep Power
    3887
    Yes, \t is for a tab. The whitespace character class (\s) will cover tabs along with other forms of whitespace (spaces, newlines, etc).

    For the record I'm not a PHPer. In a Perl forum I would normally suggest that you not try to write regexps for parsing HTML (unless you're just practicing of course) but use a proper HTML parsing library. I guarantee it'll be easier and more reliable for you.

    Regexps are not always the answer.
  6. #4
  7. Classical Computing
    Devshed Novice (500 - 999 posts)

    Join Date
    May 2008
    Location
    Melbourne, Australia
    Posts
    893
    Rep Power
    102
    Thank you both very much for your replies. Much appreciated.

    Now I am off to find out what an "HTML parsing library" is. Sounds interesting. (As I say, I'm very new at this...)

    Best wishes.
    Simple is beautiful.
  8. #5
  9. No Profile Picture
    I haz teh codez!
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Dec 2003
    Posts
    2,549
    Rep Power
    2337
    Here's one. I can't vouch for it as I've not had any needs to do HTML parsing, but it at least looks like it has some docs.
  10. #6
  11. Classical Computing
    Devshed Novice (500 - 999 posts)

    Join Date
    May 2008
    Location
    Melbourne, Australia
    Posts
    893
    Rep Power
    102
    @ptr2void: Gracias! I'll try it out.
    Simple is beautiful.

IMN logo majestic logo threadwatch logo seochat tools logo