PHP: version of non-preg_match: preg_match ("/ [^ a-z0-9] / i", $ a, $ match)?

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)){ #echo "violation at: ". substr($input, $validLen,1); } $count = $count + 1; }; $end_time = microtime(TRUE); $dif = $end_time - $start_time; echo $dif; 

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); #echo "violation at:". $m[0]; $count = $count + 1; }; $end_time = microtime(TRUE); $dif = $end_time - $start_time; echo $dif; 

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)

+5
source share
1 answer

Do you want to find the location of the first character in a non-specified range without using regular expressions? You may want strspn or its strcspn add- strcspn

 $allowed = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'; $input = 'abc-def'; $validLen = strspn($input, $allowed); if (strlen($input) !== $validLen) { printf('Input invalid, starting at %s', substr($input, $validLen)); } else { echo 'Input is valid'; } 

Output Input invalid, starting at -def . Watch it live .

strspn (and its complement) is very old, very well defined (POSIX equals). Standard implementations are optimized for this task. PHP just uses this platform, so PHP should be fast too.

+3
source

Source: https://habr.com/ru/post/1243664/


All Articles