Presumably the line:
$a = "abc-def" if (preg_match("/[^a-z0-9]/i", $a, $m)){ $i = "i stopped scanning '$a' because I found a violation in it while scanning it from left to right. The violation was: $m[0]"; } echo $i;
example above: should indicate "-" there was a violation.
I would like to know if there is a non-preg_match way to do this.
I will most likely be testing the tests if there is a non-preg_match way to do this, maybe 1000 or 1 million runs to see which is faster and more efficient.
In tests, "$ a" will be much more. To make sure that he is not trying to scan the entire "$ a" and ensure that it is terminated soon, as it detects a violation within the "$ a"
Based on the information I observed on the Internet, preg_match stops when the first match is found.
UPDATE:
this is based on the answer given by the βbishopβ and is likely to be chosen as a valid answer soon (soon).
I changed it a little, because I only want it to inform the character of the offender. but I also commented on this line, so the benchmark can work without confusion.
let it launch 1 millionth launch based on this answer.
$start_time = microtime(TRUE); $count = 0; while ($count < 1000000){ $allowed = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'; $input = 'abc-def'; $validLen = strspn($input, $allowed); if ($validLen < strlen($input)){
result: 0.606614112854 (60 percent of a second)
do this using the preg_match method.
I hope everything will be the same. (and fair) .. (I say this because there is a ^ character in preg_match)
$start_time = microtime(TRUE); $count = 0; while ($count < 1000000){ $input = 'abc-def'; preg_match("/[^a-z0-9]/i", $input, $m);
I use diff for terminological difference.
"dif" was .. 1.1145210266113
(11 percent more than a whole second)
(if it was 1.2, it would mean that it is 2 times slower than the php method)