phpBB
Statistics
| Revision:

root / trunk / phpBB / develop / regex.php

History | View | Annotate | Download (2.5 kB)

1
<?php
2
//
3
// Security message:
4
//
5
// This script is potentially dangerous.
6
// Remove or comment the next line (die(".... ) to enable this script.
7
// Do NOT FORGET to either remove this script or disable it after you have used it.
8
//
9
die("Please read the first lines of this script for instructions on how to enable it");
10
11
12
// IP regular expressions
13
14
$dec_octet = '(?:\d{1,2}|1\d\d|2[0-4]\d|25[0-5])';
15
$h16 = '[\dA-F]{1,4}';
16
$ipv4 = "(?:$dec_octet\.){3}$dec_octet";
17
$ls32 = "(?:$h16:$h16|$ipv4)";
18
19
$ipv6_construct = array(
20
        array(false,        '',                '{6}',        $ls32),
21
        array(false,        '::',        '{0,5}', "(?:$h16(?::$h16)?|$ipv4)"),
22
        array('',                ':',        '{4}',        $ls32),
23
        array('{1,2}',        ':',        '{3}',        $ls32),
24
        array('{1,3}',        ':',        '{2}',        $ls32),
25
        array('{1,4}',        ':',        '',                $ls32),
26
        array('{1,5}',        ':',        false,        $ls32),
27
        array('{1,6}',        ':',        false,        $h16),
28
        array('{1,7}',        ':',        false,        ''),
29
        array(false, '::', false, '')
30
);
31
32
$ipv6 = '(?:';
33
foreach ($ipv6_construct as $ip_type)
34
{
35
        $ipv6 .= '(?:';
36
        if ($ip_type[0] !== false)
37
        {
38
                $ipv6 .= "(?:$h16:)" . $ip_type[0];
39
        }
40
        $ipv6 .= $ip_type[1];
41
        if ($ip_type[2] !== false)
42
        {
43
                $ipv6 .= "(?:$h16:)" . $ip_type[2];
44
        }
45
        $ipv6 .= $ip_type[3] . ')|';
46
}
47
$ipv6 = substr($ipv6, 0, -1) . ')';
48
49
echo 'IPv4: ' . $ipv4 . "<br />\nIPv6: " . $ipv6 . "<br />\n";
50
51
// URL regular expressions
52
53
$pct_encoded = "%[\dA-F]{2}";
54
$unreserved = 'a-z0-9\-._~';
55
$sub_delims = '!$&\'()*+,;=';
56
$pchar = "(?:[$unreserved$sub_delims:@|]+|$pct_encoded)"; // rfc: no "|"
57
58
$scheme = '[a-z][a-z\d+\-.]*';
59
$reg_name = "(?:[$unreserved$sub_delims:@|]+|$pct_encoded)+"; // rfc: * instead of + and no "|" and no "@" and no ":" (included instead of userinfo)
60
//$userinfo = "(?:(?:[$unreserved$sub_delims:]+|$pct_encoded))*";
61
$ipv4_simple = '[0-9.]+';
62
$ipv6_simple = '\[[a-z0-9.]+:[a-z0-9.]+:[a-z0-9.:]+\]';
63
$host = "(?:$reg_name|$ipv4_simple|$ipv6_simple)";
64
$port = '\d*';
65
//$authority = "(?:$userinfo@)?$host(?::$port)?";
66
$authority = "$host(?::$port)?";
67
$segment = "$pchar*";
68
$path_abempty = "(?:/$segment)*";
69
$hier_part = "/{2}$authority$path_abempty";
70
$query = "(?:[$unreserved$sub_delims:@/?|]+|$pct_encoded)*"; // pchar | "/" | "?", rfc: no "|"
71
$fragment = $query;
72
73
$url =  "$scheme:$hier_part(?:\?$query)?(?:\#$fragment)?";
74
echo 'URL: ' . $url . "<br />\n";
75
76
// no scheme, shortened authority, but host has to start with www.
77
$www_url =  "www\.$reg_name(?::$port)?$path_abempty(?:\?$query)?(?:\#$fragment)?";
78
echo 'www.URL: ' . $www_url . "<br />\n";
79
80
// no schema and no authority
81
$relative_url = "$segment$path_abempty(?:\?$query)?(?:\#$fragment)?";
82
echo 'relative URL: ' . $relative_url . "<br />\n";