SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    Making a better wheel silver trophy DR_LaRRY_PEpPeR's Avatar
    Join Date
    Jul 2001
    Location
    Missouri
    Posts
    3,428
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    reg exp questions and what is [[:space:]] etc

    coming from Perl i have a lot of q's about PHP's regular expressions oddities (to me)...

    1) i've seen [[:space:]] and the like used in people's reg exp examples, but i don't see it mentioned anywhere on the PHP site. could you explain what it is, and what others are there like it? is it [:space:] (notice single brackets) INSIDE of a character class or is it just its own thing (i.e. ALWAYS 2 brackets on each side)? does [[:space:]] match just a space or ANY whitespace?

    i think i've seen [[:alpha:]] and [[:alphanum:]]. are there any others? can these be used in preg functions or just ereg?

    2) speaking of preg/ereg, which is better? is it personal preference or is one faster?

    3) single or double quotes around expressions? hmm, i guess you need double if you want vars to interpolate?

    i still don't understand the oddities of expressions that include backslashes when you're using single vs double quotes. for example, to match a "\":
    in dbl quotes: \\\\
    in sgl quotes: \\ (like Perl)

    i think that's right. does the same go for matching a quote (\" or \\"?) or a newline (\n or \\n?)? ugh! does preg have these issues or just ereg?

    i'm really greatful for any help on this stuff. i sure do miss Perl's s///!

  2. #2
    Node mutilating coot timnz's Avatar
    Join Date
    Feb 2001
    Location
    New Zealand
    Posts
    516
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I can answer one of your questions hopefully.
    [[:space:]] means space, or the stuff between each of my words.

  3. #3
    Making a better wheel silver trophy DR_LaRRY_PEpPeR's Avatar
    Join Date
    Jul 2001
    Location
    Missouri
    Posts
    3,428
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by timnz
    I can answer one of your questions hopefully.
    [[:space:]] means space, or the stuff between each of my words.
    he he, thanks.

    of course i'm an idiot cuz right after i posted i was looking around on the PHP site and found a couple of sites that explain these [xxxx:] things:

    www.ciser.cornell.edu/info/regex.html
    www.contactor.se/~dast/mail2sms/regex.shtml

    i found out that [:space:] isn't just a literal space, it's any whitespace char (i.e. like [ \n\r\t]). i wonder if things like [:space:] work in preg. i'll have to try it, although i had never heard of them in Perl. but i think i'd prefer to use \s instead of [:space:] anyway.

  4. #4
    Node mutilating coot timnz's Avatar
    Join Date
    Feb 2001
    Location
    New Zealand
    Posts
    516
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I hope you mean [[:space:]] and not [:space:]

  5. #5
    Making a better wheel silver trophy DR_LaRRY_PEpPeR's Avatar
    Join Date
    Jul 2001
    Location
    Missouri
    Posts
    3,428
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by timnz
    I hope you mean [[:space:]] and not [:space:]
    no, i mean [:space:]. AFAIK after looking at those 2 pages that's how it is. you would only need [[:space:]] if you had it in a char class with something else. such as [[:space:][:alpha:]], i think. correct me if i'm wrong...

  6. #6
    midnight coder
    Join Date
    Dec 2000
    Location
    The flat edge of the world
    Posts
    838
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    double quotes interploats the variables, eg

    PHP Code:
    $var robo;

    echo 
    'hi, my name is $var';
    // prints hi, my name is $var

    echo "hi, my name is $var";
    // prints hi, my name is robo 
    And a \ escapes the next chracter to be process as PHP, say you wrote:

    echo "<a href="home.htm">Go home</a>";

    You'll get an error 'cos PHP stops echoing after the second quote behind href= and doesn't know what to do with the test following it, so you have to go:

    echo "<a href=\"home.htm\">Go home</a>";

    This will give you the correct link.

    And as you know, \n makes a new line, but if you really want to print "\n" on the screen, you'll have to go:

    echo "\\n";

    Things get slightly messy when you want to print say part of an array like $array[1][0] along with html code using echo, you'll go:

    //$array[1][0] gives home.htm
    echo "<a href=\"" . $array[1][0] . "\">Go home</a>";

    Hope that helped you a bit.

  7. #7
    Making a better wheel silver trophy DR_LaRRY_PEpPeR's Avatar
    Join Date
    Jul 2001
    Location
    Missouri
    Posts
    3,428
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks Robo, but i know the difference between single and double quotes in those situations. i'm just not positive about the differences between the 2 in a regular expression. i'm probably gonna have to figure it out myself by trial and error since it's probably to hard for someone to explain the differences if there's more than a couple.

    now i want to know if you all think ereg or preg is better.

  8. #8
    Node mutilating coot timnz's Avatar
    Join Date
    Feb 2001
    Location
    New Zealand
    Posts
    516
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well I was only guessing, you learn something everyday, I learnt not to guess to much today.

  9. #9
    Making a better wheel silver trophy DR_LaRRY_PEpPeR's Avatar
    Join Date
    Jul 2001
    Location
    Missouri
    Posts
    3,428
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i think i found out that the preg functions are better (faster) than ereg. i ran a loop 10,000 times doing a replacement (perg_replace(), ereg_replace()) on a short string and preg_replace was almost TWICE as fast. awesome!


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
  •