<?php
function testRegExp($regexp, $string) {
$valid = preg_match($regexp, $string);
echo $string. " - " . ($valid ? "valid" : "invalid") ."\n";
}
function matchRegExp($regexp, $text){
if(preg_match($regexp, $text, $matches)){
print_r($matches);
}else{
echo "no match in text: ".$text;
}
}
// hledání emailu @seznam.cz v textu (zjednodušená verze)
$regexpEmail = "/\b(?P<email>\w+@seznam\.cz)\b/";
matchRegExp($regexpEmail, "adsd dominik@seznam.cz ssdasd");
matchRegExp($regexpEmail, "adsd dominik@seam.cz ssdasd");
// validace IP adresy
$regexpIp = "/^((25[0-5]|2[0-4]\d|[01]?\d?\d)\.){3}(25[0-5]|2[0-4]\d|[01]?\d?\d)$/";
echo "Test IP\n";
testRegExp($regexpIp, "10.1.0.1");
testRegExp($regexpIp, "199.1.0.1");
testRegExp($regexpIp, "255.1.0.1");
testRegExp($regexpIp, "256.1.0.1");
testRegExp($regexpIp, "260.1.0.1");
testRegExp($regexpIp, "300.1.0.1");
testRegExp($regexpIp, "1.0.1");
testRegExp($regexpIp, "125.1..1");
testRegExp($regexpIp, "192.11.168.255");
testRegExp($regexpIp, "192.11.168.256");
testRegExp($regexpIp, "192.11.168.300");
- Output for 5.6.0 - 5.6.27, 7.0.0 - 7.0.20, 7.1.0 - 7.1.7, 7.2.0 - 7.2.33, 7.3.16 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.18, 8.3.0 - 8.3.4, 8.3.6
- Array
(
[0] => dominik@seznam.cz
[email] => dominik@seznam.cz
[1] => dominik@seznam.cz
)
no match in text: adsd dominik@seam.cz ssdasdTest IP
10.1.0.1 - valid
199.1.0.1 - valid
255.1.0.1 - valid
256.1.0.1 - invalid
260.1.0.1 - invalid
300.1.0.1 - invalid
1.0.1 - invalid
125.1..1 - invalid
192.11.168.255 - valid
192.11.168.256 - invalid
192.11.168.300 - invalid
- Output for 8.3.5
- Warning: PHP Startup: Unable to load dynamic library 'sodium.so' (tried: /usr/lib/php/8.3.5/modules/sodium.so (libsodium.so.23: cannot open shared object file: No such file or directory), /usr/lib/php/8.3.5/modules/sodium.so.so (/usr/lib/php/8.3.5/modules/sodium.so.so: cannot open shared object file: No such file or directory)) in Unknown on line 0
Array
(
[0] => dominik@seznam.cz
[email] => dominik@seznam.cz
[1] => dominik@seznam.cz
)
no match in text: adsd dominik@seam.cz ssdasdTest IP
10.1.0.1 - valid
199.1.0.1 - valid
255.1.0.1 - valid
256.1.0.1 - invalid
260.1.0.1 - invalid
300.1.0.1 - invalid
1.0.1 - invalid
125.1..1 - invalid
192.11.168.255 - valid
192.11.168.256 - invalid
192.11.168.300 - invalid
preferences:
181.47 ms | 402 KiB | 186 Q