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