Subversion Repositories javautils

Rev

Rev 4 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
4 daniel-mar 1
package com.dominicsayers.isemail;
2
 
3
import java.util.regex.Matcher;
4
import java.util.regex.Pattern;
5
 
6
/**
7
 * IMPORTANT NOTE! These functions were developed during the translation process
8
 * of the E-Mail-Address verification class for Dominic Sayers. These functions
9
 * are NEITHER AN IDENTICAL NOR A OFFICIAL equivalence of PHP's functions. The
10
 * functionality is only as much as needed by my initial purpose. Special cases
11
 * are usually not implemented. Please also note that you have to use the JAVA
12
 * REGULAR EXPRESSION syntax! PHP's PCRE IS NOT INTERPRETED OR CONVERTED!
13 daniel-mar 13
 * @author Daniel Marschall
14
 * @version 2010-06-14
4 daniel-mar 15
 */
16
 
17
public class PHPFunctions {
18
 
19
        public static int preg_match(String regex, String input) {
20
                Matcher m = Pattern.compile(regex).matcher(input);
21
 
22
                int c = 0;
23
                while (m.find()) {
24
                        return 1; // preg_match() bricht bei erster Übereinstimmung ab.
25
                }
26
                return c;
27
        }
28
 
29
        public static String[] preg_match_to_array(String regex, String input) {
30
                Matcher m = Pattern.compile(regex).matcher(input);
31
 
32
                if (m.find()) {
33
                        String[] result = new String[m.groupCount() + 1];
34
                        for (int i = 0; i < result.length; i++) {
35
                                result[i] = m.group(i);
36
                        }
37
                        return result;
38
                } else {
39
                        return new String[0];
40
                }
41
        }
42
 
43
        public static String[] preg_split(String regex, String input) {
44
                return input.split(regex, -1);
45
        }
46
 
47
        /**
48
         * @returns [group#][match#]
49
         */
50
        private static String[] appendToStringArray(String[] ary, String append) {
51
                if (ary == null)
52
                        ary = new String[0];
53
                String[] ary2 = new String[ary.length + 1];
54
 
55
                for (int i = 0; i < ary.length; i++) {
56
                        ary2[i] = ary[i];
57
                }
58
                ary2[ary.length] = append;
59
 
60
                return ary2;
61
        }
62
 
63
        public static String[][] preg_match_all(String regex, String input) {
64
                Matcher m = Pattern.compile(regex).matcher(input);
65
 
66
                if (m.find()) {
67
                        int j = -1;
68
 
69
                        String[][] result = new String[m.groupCount() + 1][];
70
                        do {
71
                                j++;
72
 
73
                                for (int i = 0; i < result.length; i++) {
74
                                        result[i] = appendToStringArray(result[i], m.group(i));
75
                                }
76
                        } while (m.find());
77
 
78
                        return result;
79
                } else {
80
                        return new String[0][0];
81
                }
82
        }
83
 
84
        public static String substr(String input, int start) {
85
                return input.substring(start);
86
        }
87
 
88
        public static String substr(String input, int start, int length) {
89
                return input.substring(start, start + length);
90
        }
91
 
92
        public static String preg_replace(String pattern, String replacement,
93
                        String subject) {
94
                return subject.replaceAll(pattern, replacement);
95
        }
96
 
97
        private PHPFunctions() {
98
        }
99
 
100
}