Hello all,

How can I check if a string contain:
No capitals
No spaces
No quotes
5-14 characters
no spaces

?

thanks!

    Phrase it another way: what do you want the string to contain, and nothing else?

      This can be solved utilizing reg expressions and the strlen function.

      Below is some sample code:

      <?php

      $string = "dogrun";

      if (!preg_match("/([A-Z]+|\s+|'|\")/", $string) && (strlen($string) > 4) && (strlen($string) < 15)) {
      echo 'string matches criteria';
      } else {
      echo 'string fails criteria';
      }

      ?>

      [Edit] I forgot to give this link for some reading you can do on the subject: http://www.webcheatsheet.com/php/regular_expressions.php

      Thanks!
      Nathan

        nrasch wrote:

        This can be solved utilizing reg expressions and the strlen function.

        There would be no need for strlen() since the regex pattern can account for the number of characters.

        I want scialom to state the requirements positively as it is often better to check that the input is valid rather than check that the input is not invalid since one might have left out something when specifying what is invalid.

          Nathan, I agree with Laserlight in waiting to get the absolute criteria.. but in the interim, the solution can also be used as such (based on what you have done):

          $string = "dogun";
          echo (preg_match('#^[^A-Z\s\'"]{5,14}$#', $string))? 'string matches criteria' : 'string fails criteria';
          

            Hrmm, I'm going to stick by the way I did it: Readable, gets the job done, allows for multiple lines of execution based on pass|fail of testing, etc.

            Everyone does things differently, and my solution is just as good as the ones you suggest. πŸ™‚

            Many thanks!
            Nathan

              Hrmm, let's try this again; my last post got eaten.

              I'm going to stick by the way I did it. It's readable, allows mutiple lines of execution after the pass|fail test, and so forth.

              There are many different ways of doing things, and my solution is just as correct as any other.

              Thanks!
              Nathan

                Feynman's other dictum: "Your way is always better until something goes wrong".

                <?php
                
                $string = "&#8364;12.30";
                
                if (!preg_match("/([A-Z]+|\s+|'|\")/", $string) && (strlen($string) > 4) && (strlen($string) < 15)) {
                echo 'string matches criteria';
                } else {
                echo 'string fails criteria';
                }
                
                ?>

                  Uhhh, cute quote. πŸ˜‰

                  However, you missed the requirements: The user made no mention of currency symbols or periods. My code correctly passes your example based on what was defined.

                  Good try tho; requirements can sometimes be tricky to understand. This is a great real life example. πŸ™‚

                  Again, my way is just as good as anyone else's solution; which you choose is solely a matter of preference.

                  Oh, I almost forgot: Thanks for all the warm welcomes to the forum. πŸ™‚ You all have certainly made me feel right at home here. πŸ˜‰

                  [Edit] I forgot to mention that you can use this site here http://www.spaweditor.com/scripts/regex/ to test things like that example you came up with to see how it works ahead of time. I think you'll find it useful when trying to come up with test cases.... πŸ™‚

                  Thanks!
                  Nathan

                    As laserlight said "it is often better to check that the input is valid rather than check that the input is not invalid since one might have left out something when specifying what is invalid."

                    <?php
                    
                    $string = "`Γ…ndersson`";
                    
                    if (!preg_match("/([A-Z]+|\s+|'|\")/", $string) && (strlen($string) > 4) && (strlen($string) < 15)) {
                    echo 'string matches criteria';
                    } else {
                    echo 'string fails criteria';
                    }
                    
                    ?> 
                      nrasch wrote:

                      Hrmm, I'm going to stick by the way I did it: Readable, gets the job done, allows for multiple lines of execution based on pass|fail of testing, etc.

                      I think that they are both readable and get the job done, though I find nrg_alpha's version more readable as it makes good use of a character class (you do not need to check for one or more uppercase letters and whitespace, after all, since you only need to check for one). I am not sure what you mean by "allows for multiple lines of execution based on pass|fail of testing".

                      Incidentally, perhaps the word "space" should be clarified. Does it refer to a space character, or to whitespace in general? Currently both nrasch and nrg_alpha assume that it means whitespace.

                      nrasch wrote:

                      However, you missed the requirements: The user made no mention of currency symbols or periods. My code correctly passes your example based on what was defined.

                      Yeah, that is precisely what I mean. According to the currently given requirements, you are correct, but there is also the lingering doubt that perhaps scialom merely did not consider what would happen if a user entered a euro symbol, a numeral or a period.

                      nrasch wrote:

                      You all have certainly made me feel right at home here.

                      Heh πŸ˜ƒ

                        nrasch;10900258 wrote:

                        Hrmm, I'm going to stick by the way I did it: Readable, gets the job done, allows for multiple lines of execution based on pass|fail of testing, etc.

                        Hi Nathan...don't get me wrong, I wasn't trying to sway you over to my solution or anything, hence the wording in my initial post:

                        ...the solution can also be...

                        Being how your example was merely printing out a simple string given either the true or false results, I simply streamlined everything using ternary operator notaion...(which IMO isn't hard to read... but admittedly, this is subjective) but yeah, I could have easily resorted to if(...){....}else{...} format as well.

                        Everyone does things differently, and my solution is just as good as the ones you suggest. πŸ™‚

                        Actually, there is one very small difference.. speed of execution. Mine edges out in a timed test.. but in reality, on a single pass, the difference would in fact be infinitesimal. Before I go further, allow me to aknowledge that in general, functions like strlen, strpos, ctype_digit and the like are exceptionally fast.. often non-regex solutions outstrip regex in raw speed (but as we know, regex sacrifices speed for robust flexibility and power). But even so, no matter which way you choose, if not structured nor used wisely, you may lose some relative speed performance.

                        If I may offer some critique / advice (without the intention of insulting you here, as this is not what PHP Builder is about).. Let's re-examine your solution and breakdown it's workflow:

                        $string = "dogrun";
                        
                        if (!preg_match("/([A-Z]+|\s+|'|\")/", $string) && (strlen($string) > 4) && (strlen($string) < 15)) {
                        echo 'string matches criteria';
                        } else {
                        echo 'string fails criteria';
                        }
                        

                        You start off by using a pattern that is largely alternation based (...|...). Alternations are less favorable than say a character class [..] due to it being slower... if you look at my solution, there are no alternations.. just one negated character class (which executes faster). I am also seeing that you have executed strlen twice... it is more efficient to create a variable (say $stringLength), store strlen's result into that, then use that in multiple places instead of strlen()...

                        this is akin to a for loop.. you probably often see things like:

                        for($i = 0; $i < count($arr); $i++){...
                        

                        This means that everytime the loop checks the conditional part, it has to recalculate the count() functionality.. If however we did this instead:

                        for($i = 0, $total = count($arr); $i < $total; $i++){...
                        

                        The total count is only calculated once when the loop is initiated.. from there on out, the condition is using a variable that is based on a calculation which was done only once.. so we shave off some time.. So this is what I am getting at with regards to your (strlen($string) > 4) && (strlen($string) < 15) part.
                        But if you can merge this functionality into your regex to begin with, you streamline things, as using {x,y} isn't severely heavy in calculation speed..

                        Again, all in all.. one a single (or small batch of content to check), the speed differences will probably be imperceptible to the user.. but if you wish to code to stricter / faster ethics, it's something to consider..

                        Finally, if you really wish to get on the ball with regex and understand what is truly going on 'beneath the hood', I would strongly recommend picking up this book. It goes beyond what other regex tutorial sites do by explaining the mechanics of regex, as in how it thinks... You will walk away with a much greater understanding which would enable you to exploit regex's speed better.

                        In closing, welcome to PHP builder! πŸ™‚ Mods like Laserlight and WP are extremely knowledgable and will certainly bolster your learning here (I know they contributed in mine, with more to come no doubt!).

                        Cheers

                          laserlight wrote:

                          There would be no need for strlen() since the regex pattern can account for the number of characters.

                          I'd still retain the string length test, and I'd test the length first. Reason being, that information is already available and can be looked at immediately and acted on without the overhead of NFA construction.

                          But I still want a proper specification. If this was the real world I'd be hassling the so-called stakeholder for clarification about that. Not just working to rule.

                            Not to poke the bear or anything, but why not use the built in regex {4,15}?

                            preg_match("/([A-Z]+|\s+|'|\"){4,15}/", $string)
                              Kudose wrote:

                              Not to poke the bear or anything, but why not use the built in regex {4,15}?

                              Tsk, you should read the thread before poking the bear πŸ™‚

                              Anyway, that will not work, since that checks for 4 to 15 of the invalid characters when you only need to check if there is one of them.

                                laserlight;10900405 wrote:

                                Tsk, you should read the thread before poking the bear πŸ™‚

                                I was hoping to get away with reading 3/4. Shame on me. πŸ™‚

                                  nrasch;10900276 wrote:

                                  However, you missed the requirements: The user made no mention of currency symbols or periods. My code correctly passes your example based on what was defined.

                                  Good try tho; requirements can sometimes be tricky to understand. This is a great real life example. πŸ™‚

                                  Again, my way is just as good as anyone else's solution; which you choose is solely a matter of preference.

                                  You are right. I mean, I thought that you didn't wanted to allow japanese characters, but apparently they should be allowed. My mistake.

                                  Basically a whitelist allow you to specify what characters to use while a blacklist specify the characters that are not allowed. If you have to add a character to a whitelist it is no big problem; if you have to add a character to the blacklist the harm is already done.

                                    Hmmm....I wonder where this thread ranks in terms of number of replies without any being from the original poster? πŸ™‚

                                      I'm sure it doesn't come close to the word association game thread. πŸ™‚

                                        Kudose;10900473 wrote:

                                        I'm sure it doesn't come close to the word association game thread. πŸ™‚

                                        Well, the OP in that thread had his first reply at post #18 in it, so this thread now beats it in terms of number of replies before the OP's first reply, should he ever actually reply.