I'm creating a text interpreter based on the DuckDuckGo Github documentation > in PHP.
This is one of the codes I created:
if (strpos(strtolower($qt), "rand") !== FALSE){
if (preg_match("/^rand$/", strtolower(removeAccents($qt)), $match)){
$result = rand(1,9999);
$sndline = "Random number";
}
elseif (preg_match("/^random$/", strtolower(removeAccents($qt)), $match)){
$result = rand(1,9999);
$sndline = "Random number";
}
elseif (preg_match("/^rand *\((?<min>[0-9]+),(?<max>[0-9]+)\)$/", strtolower(removeAccents($qt)), $match)){
$result = rand($match['min'],$match['max']);
$sndline = "Random number";
}
elseif (preg_match("/^random *\((?<min>[0-9]+),(?<max>[0-9]+)\)$/", strtolower(removeAccents($qt)), $match)){
$result = rand($match['min'],$match['max']);
$sndline = "Random number";
}
}
As you can see, the script performs the action if the user types rand
, random
, rand (1,99)
and random (1,99)
.
The entire interpreter was written with preg_match
functions (that is, everything with RegEx) but found that they overloaded the system.
How can I create something that is fast (without overloading the system) and also understands different "orders" given by the user without using RegEx (remembering that the user can type anything in the search)?