'SEO friendly links'
ik maak mijn links url vriendelijk door gebruik te maken van het volgende:
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
function generateLink($string) {
$chars = array('' => 'A',
'' => 'A',
'' => 'A',
'' => 'A',
'' => 'A',
'' => 'A',
'' => 'AE',
'' => 'C',
'' => 'E',
'' => 'E',
'' => 'E',
'' => 'I',
'' => 'I',
'' => 'I',
'' => 'I',
'' => 'D',
'' => 'N',
'' => 'O',
'' => 'O',
'' => 'O',
'' => 'O',
'' => 'O',
'' => 'x',
'' => 'o',
'' => 'U',
'' => 'U',
'' => 'U',
'' => 'U',
'' => 'Y',
'' => 'p',
'' => 'ss',
'' => 'a',
'' => 'a',
'' => 'a',
'' => 'a',
'' => 'a',
'' => 'a',
'' => 'ae',
'' => 'c',
'' => 'e',
'' => 'e',
'' => 'e',
'' => 'e',
'' => 'i',
'' => 'i',
'' => 'i',
'' => 'i',
'' => 'o',
'' => 'n',
'' => 'o',
'' => 'o',
'' => 'o',
'' => 'o',
'' => 'o',
'' => 'o',
'' => 'u',
'' => 'u',
'' => 'u',
'' => 'u',
'' => 'y',
'' => 'p',
'' => 'y',
'' => 'CE',
'' => 'ce',
'' => 'S',
'' => 's',
'' => 'Y',
'' => 'f',
'À' => 'A',
'Á' => 'A',
'Â' => 'A',
'Ã' => 'A',
'Ä' => 'A',
'Å' => 'A',
'Æ' => 'AE',
'Ç' => 'C',
'È' => 'E',
'É' => 'E',
'Ê' => 'E',
'Ë' => 'E',
'Ì' => 'I',
'Í' => 'I',
'Î' => 'I',
'Ï' => 'I',
'Ð' => 'D',
'Ñ' => 'N',
'Ò' => 'O',
'Ó' => 'O',
'Ô' => 'O',
'Õ' => 'O',
'Ö' => 'O',
'×' => 'x',
'Ø' => 'o',
'Ù' => 'U',
'Ú' => 'U',
'Û' => 'U',
'Ü' => 'U',
'Ý' => 'Y',
'Þ' => 'p',
'ß' => 'ss',
'à' => 'a',
'á' => 'a',
'â' => 'a',
'ã' => 'a',
'ä' => 'a',
'å' => 'a',
'æ' => 'ae',
'ç' => 'c',
'è' => 'e',
'é' => 'e',
'ê' => 'e',
'ë' => 'e',
'ì' => 'i',
'í' => 'i',
'î' => 'i',
'ï' => 'i',
'ð' => 'o',
'ñ' => 'n',
'ò' => 'o',
'ó' => 'o',
'ô' => 'o',
'õ' => 'o',
'ö' => 'o',
'ø' => 'o',
'ù' => 'u',
'ú' => 'u',
'û' => 'u',
'ü' => 'u',
'ý' => 'y',
'þ' => 'p',
'ÿ' => 'y',
'À' => 'A',
'Á' => 'A',
'Â' => 'A',
'Ã' => 'A',
'Ä' => 'A',
'Å' => 'A',
'Æ' => 'AE',
'Ç' => 'C',
'È' => 'E',
'É' => 'E',
'Ê' => 'E',
'Ë' => 'E',
'Ì' => 'I',
'Í' => 'I',
'Î' => 'I',
'Ï' => 'I',
'Ð' => 'D',
'Ñ' => 'N',
'Ò' => 'O',
'Ó' => 'O',
'Ô' => 'O',
'Õ' => 'O',
'Ö' => 'O',
'×' => 'x',
'Ø' => 'o',
'Ù' => 'U',
'Ú' => 'U',
'Û' => 'U',
'Ü' => 'U',
'Ý' => 'Y',
'Þ' => 'p',
'ß' => 'ss',
'à' => 'a',
'á' => 'a',
'â' => 'a',
'ã' => 'a',
'ä' => 'a',
'å' => 'a',
'æ' => 'ae',
'ç' => 'c',
'è' => 'e',
'é' => 'e',
'ê' => 'e',
'ë' => 'e',
'ì' => 'i',
'í' => 'i',
'î' => 'i',
'ï' => 'i',
'ð' => 'o',
'ñ' => 'n',
'ò' => 'o',
'ó' => 'o',
'ô' => 'o',
'õ' => 'o',
'ö' => 'o',
'ø' => 'o',
'ù' => 'u',
'ú' => 'u',
'û' => 'u',
'ü' => 'u',
'ý' => 'y',
'þ' => 'p',
'ÿ' => 'y',
'Œ' => 'CE',
'œ' => 'ce',
'Š' => 'S',
'š' => 's',
'Ÿ' => 'Y',
'ƒ' => 'f',
"'" => '',
'"' => '',
'`' => '',
'/' => '',
'\\' => '');
foreach($chars as $search => $replace)
$string = str_replace($search,$replace,$string);
return $string;
}
$chars = array('' => 'A',
'' => 'A',
'' => 'A',
'' => 'A',
'' => 'A',
'' => 'A',
'' => 'AE',
'' => 'C',
'' => 'E',
'' => 'E',
'' => 'E',
'' => 'I',
'' => 'I',
'' => 'I',
'' => 'I',
'' => 'D',
'' => 'N',
'' => 'O',
'' => 'O',
'' => 'O',
'' => 'O',
'' => 'O',
'' => 'x',
'' => 'o',
'' => 'U',
'' => 'U',
'' => 'U',
'' => 'U',
'' => 'Y',
'' => 'p',
'' => 'ss',
'' => 'a',
'' => 'a',
'' => 'a',
'' => 'a',
'' => 'a',
'' => 'a',
'' => 'ae',
'' => 'c',
'' => 'e',
'' => 'e',
'' => 'e',
'' => 'e',
'' => 'i',
'' => 'i',
'' => 'i',
'' => 'i',
'' => 'o',
'' => 'n',
'' => 'o',
'' => 'o',
'' => 'o',
'' => 'o',
'' => 'o',
'' => 'o',
'' => 'u',
'' => 'u',
'' => 'u',
'' => 'u',
'' => 'y',
'' => 'p',
'' => 'y',
'' => 'CE',
'' => 'ce',
'' => 'S',
'' => 's',
'' => 'Y',
'' => 'f',
'À' => 'A',
'Á' => 'A',
'Â' => 'A',
'Ã' => 'A',
'Ä' => 'A',
'Å' => 'A',
'Æ' => 'AE',
'Ç' => 'C',
'È' => 'E',
'É' => 'E',
'Ê' => 'E',
'Ë' => 'E',
'Ì' => 'I',
'Í' => 'I',
'Î' => 'I',
'Ï' => 'I',
'Ð' => 'D',
'Ñ' => 'N',
'Ò' => 'O',
'Ó' => 'O',
'Ô' => 'O',
'Õ' => 'O',
'Ö' => 'O',
'×' => 'x',
'Ø' => 'o',
'Ù' => 'U',
'Ú' => 'U',
'Û' => 'U',
'Ü' => 'U',
'Ý' => 'Y',
'Þ' => 'p',
'ß' => 'ss',
'à' => 'a',
'á' => 'a',
'â' => 'a',
'ã' => 'a',
'ä' => 'a',
'å' => 'a',
'æ' => 'ae',
'ç' => 'c',
'è' => 'e',
'é' => 'e',
'ê' => 'e',
'ë' => 'e',
'ì' => 'i',
'í' => 'i',
'î' => 'i',
'ï' => 'i',
'ð' => 'o',
'ñ' => 'n',
'ò' => 'o',
'ó' => 'o',
'ô' => 'o',
'õ' => 'o',
'ö' => 'o',
'ø' => 'o',
'ù' => 'u',
'ú' => 'u',
'û' => 'u',
'ü' => 'u',
'ý' => 'y',
'þ' => 'p',
'ÿ' => 'y',
'À' => 'A',
'Á' => 'A',
'Â' => 'A',
'Ã' => 'A',
'Ä' => 'A',
'Å' => 'A',
'Æ' => 'AE',
'Ç' => 'C',
'È' => 'E',
'É' => 'E',
'Ê' => 'E',
'Ë' => 'E',
'Ì' => 'I',
'Í' => 'I',
'Î' => 'I',
'Ï' => 'I',
'Ð' => 'D',
'Ñ' => 'N',
'Ò' => 'O',
'Ó' => 'O',
'Ô' => 'O',
'Õ' => 'O',
'Ö' => 'O',
'×' => 'x',
'Ø' => 'o',
'Ù' => 'U',
'Ú' => 'U',
'Û' => 'U',
'Ü' => 'U',
'Ý' => 'Y',
'Þ' => 'p',
'ß' => 'ss',
'à' => 'a',
'á' => 'a',
'â' => 'a',
'ã' => 'a',
'ä' => 'a',
'å' => 'a',
'æ' => 'ae',
'ç' => 'c',
'è' => 'e',
'é' => 'e',
'ê' => 'e',
'ë' => 'e',
'ì' => 'i',
'í' => 'i',
'î' => 'i',
'ï' => 'i',
'ð' => 'o',
'ñ' => 'n',
'ò' => 'o',
'ó' => 'o',
'ô' => 'o',
'õ' => 'o',
'ö' => 'o',
'ø' => 'o',
'ù' => 'u',
'ú' => 'u',
'û' => 'u',
'ü' => 'u',
'ý' => 'y',
'þ' => 'p',
'ÿ' => 'y',
'Œ' => 'CE',
'œ' => 'ce',
'Š' => 'S',
'š' => 's',
'Ÿ' => 'Y',
'ƒ' => 'f',
"'" => '',
'"' => '',
'`' => '',
'/' => '',
'\\' => '');
foreach($chars as $search => $replace)
$string = str_replace($search,$replace,$string);
return $string;
}
Nou ben ik er eigenlijk heilig van overtuigd dat dit veel beter en sneller kan.
Iemand enig idee voor een beter alternatief voor bovenstaande ?
Ik eerlijk gezegd nog niet lang genoeg gegoogled hiervoor.
http://php.net/manual/en/function.iconv.php
Mocht je toch gaan voor de oplossing van Stackoverflow met al die characters in een array: zorg dan dat je je phpscript ook opslaat in de juiste character encoding.
@Ivo, de iconv, werk je daar vaker mee ?
Ik zal een paar testjes hiermee doen :)
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<?php
// Remove query string parameters
$uri = explode('?', $uri)[0];
// Enforce lowercase URLs
$uri = mb_strtolower($uri, 'UTF-8');
// Drop common webserver directory indexes
$uri = str_replace(array('default.aspx', 'default.asp', 'index.html', 'index.htm', 'index.shtml', 'index.php'), null, $uri);
// Strip common extensions
$uri = str_replace(array('.aspx', '.asp', '.html', '.htm', '.jsp', '.php'), null, $uri);
// Replace special characters
$uri = preg_replace('~[^\\pL\d.]+~u', '-', $uri);
$uri = trim($uri, '-');
$uri = iconv('UTF-8', 'US-ASCII//TRANSLIT//IGNORE', $uri);
$uri = str_ireplace(array('"', '`', '^', '~'), null, $uri);
$uri = urlencode($uri);
?>
// Remove query string parameters
$uri = explode('?', $uri)[0];
// Enforce lowercase URLs
$uri = mb_strtolower($uri, 'UTF-8');
// Drop common webserver directory indexes
$uri = str_replace(array('default.aspx', 'default.asp', 'index.html', 'index.htm', 'index.shtml', 'index.php'), null, $uri);
// Strip common extensions
$uri = str_replace(array('.aspx', '.asp', '.html', '.htm', '.jsp', '.php'), null, $uri);
// Replace special characters
$uri = preg_replace('~[^\\pL\d.]+~u', '-', $uri);
$uri = trim($uri, '-');
$uri = iconv('UTF-8', 'US-ASCII//TRANSLIT//IGNORE', $uri);
$uri = str_ireplace(array('"', '`', '^', '~'), null, $uri);
$uri = urlencode($uri);
?>
Je moet even testen, aan de hand van een lijst met URL's, of dit script de gewenste format oplevert. Het "probleem" daarbij is dat Google en de meeste moderne browsers gewoon UTF-8 accepteren. De conversie van bijvoorbeeld ü met een umlaut naar u of ue voer je vooral uit voor oude clients (en is nog taal-afhankelijk ook).