Simple ereg help

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • ollie.mitch@gmail.com

    Simple ereg help

    Hi,

    I need two ereg expressions in my PHP code. One of them needs to check
    that a string only contains letters, and the other needs to check that
    the string only contains letters and commas (only one comma at each
    time).

    I thought that the code for only containing letters would be:

    eregi("^([a-z])", $keywords);

    But this only appears to be checking the first character.

    I also have no idea how to do the one that also allows commas (though
    not for lack of trying - believe me!)


    So, basically, I want one expression that only lets in letters:
    ie. hyasdlhlasdhl but not fhdilfd7800asda ds;'

    and one expression that only lets in letters and commas (one at a
    time):
    ie. hasiaks,asdas,a dsads but not hdasl,,ahodsa,a ds

    Thanks alot.

  • Rik

    #2
    Re: Simple ereg help

    ollie.mitch@gma il.com wrote:
    Hi,
    >
    I need two ereg expressions in my PHP code. One of them needs to check
    that a string only contains letters, and the other needs to check that
    the string only contains letters and commas (only one comma at each
    time).
    >
    I thought that the code for only containing letters would be:
    >
    eregi("^([a-z])", $keywords);
    Use the preg-variant, it's faster.

    preg_match('/^[a-z]+$/i',$text);
    and one expression that only lets in letters and commas (one at a
    time):
    ie. hasiaks,asdas,a dsads but not hdasl,,ahodsa,a ds
    preg_match('/^[a-z]+(,[a-z]+)*?$/i',$text(;

    Grtz,

    --
    Rik Wasmus


    Comment

    • friglob

      #3
      Re: Simple ereg help

      ollie.mitch@gma il.com wrote:
      Hi,
      >
      I need two ereg expressions in my PHP code. One of them needs to check
      that a string only contains letters, and the other needs to check that
      the string only contains letters and commas (only one comma at each
      time).
      >
      I thought that the code for only containing letters would be:
      >
      eregi("^([a-z])", $keywords);
      >
      But this only appears to be checking the first character.
      >
      I also have no idea how to do the one that also allows commas (though
      not for lack of trying - believe me!)
      >
      >
      So, basically, I want one expression that only lets in letters:
      ie. hyasdlhlasdhl but not fhdilfd7800asda ds;'
      >
      eregi("/^[a-z]*$/",$keywords );

      * null or more
      + one or more
      and one expression that only lets in letters and commas (one at a
      time):
      ie. hasiaks,asdas,a dsads but not hdasl,,ahodsa,a ds
      >
      eregi("[a-z]+,{1}",$keyword s);
      Thanks alot.
      >

      Comment

      • John Dunlop

        #4
        Re: Simple ereg help

        ollie.mitch@gma il.com:
        I need two ereg expressions in my PHP code. One of them needs to check
        that a string only contains letters,
        'letters' means A-Z?

        (untested)

        preg_match('/\A[a-z]+\z/i',$subject)

        Meaning: match, starting at the beginning and finishing at the end,
        one or more letters, case insensitive.
        and the other needs to check that the string only contains
        letters and commas (only one comma at each time).
        (untested)

        preg_match('/\A(,(?!,)|[a-z]*)+\z/i',$subject)

        Meaning: match, one or more times, a comma that is not followed by
        another comma or match zero or more letters.
        I thought that the code for only containing letters would be:
        >
        eregi("^([a-z])", $keywords);
        >
        But this only appears to be checking the first character.
        Yeah, you haven't quantified the character class, e.g., /^[a-z]+/, or
        anchored the pattern to the end of the subject, e.g., /^[a-z]+$/.

        --
        Jock

        Comment

        • Pedro Graca

          #5
          Re: Simple ereg help

          ollie.mitch@gma il.com wrote:
          So, basically, I want one expression that only lets in letters:
          ie. hyasdlhlasdhl but not fhdilfd7800asda ds;'
          Would you consider using preg_* instead of ereg?


          Is "ñ" a letter? Is "" a string containing only letters?
          Assuming 'yes' to both questions, try

          if (preg_match('/^[[:alpha:]]*$/', $variable)) {
          echo 'All letters.';
          } else {
          echo 'Not all letters.';
          }
          and one expression that only lets in letters and commas (one at a
          time):
          ie. hasiaks,asdas,a dsads but not hdasl,,ahodsa,a ds
          Can the string start or end with a (single) comma?
          Assuming 'no', try

          if (preg_match('/^[[:alpha:]]+(?:,[[:alpha:]]+)?$/', $variable)) {
          echo 'Match';
          } else {
          echo 'No match.';
          }

          --
          File not found: (R)esume, (R)etry, (R)erun, (R)eturn, (R)eboot

          Comment

          Working...