-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstring_escaping.libr.php
More file actions
325 lines (261 loc) · 6.98 KB
/
string_escaping.libr.php
File metadata and controls
325 lines (261 loc) · 6.98 KB
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
<?php
/**
This file is part of DevOrSysAdminScripts library.
DevOrSysAdminScripts is free software:
you can redistribute it and/or modify it under the terms
of the GNU Lesser General Public License
as published by the Free Software Foundation,
either version 3 of the License,
or (at your option) any later version.
DevOrSysAdminScripts is distributed in the hope
that it will be useful,
but WITHOUT ANY WARRANTY;
without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU Lesser General Public License for more details.
You should have received a copy of
the GNU Lesser General Public License
along with DevOrSysAdminScripts.
If not, see <https://www.gnu.org/licenses/>.
©Copyright 2023-2026 Laurent Frédéric Bernard François Lyaudet
@category Library
@package DevOrSysAdminScripts
@author Laurent Lyaudet <laurent.lyaudet@gmail.com>
@copyright 2023-2026 Laurent Frédéric Bernard François Lyaudet
@license https://www.gnu.org/licenses/lgpl-3.0.html LGPLv3+
*/
declare(strict_types=1);
declare(encoding='UTF-8');
/**
The namespace string_escaping is splitted into subnamespaces for each
language.
This subnamespace is for HTML (surprised?)...
@package DevOrSysAdminScripts
@subpackage string_escaping\HTML
*/
namespace string_escaping\HTML {
/**
When you want that the string appears as is in HTML text block.
@param string $s_string The string you want to escape.
@return string The escaped string.
*/
function escape_text(string $s_string) : string {
return str_replace(
[
'&',
'<',
],
[
'&',
'<',
],
$s_string,
);
}//end escape_text()
/**
When you want that the string appears as is in an HTML tag attribute
that starts and ends with double quotes: my_attribute="something".
Example: a title attribute.
/!\ in double quotes /!\ my_attribute='something' is not supported.
@param string $s_string The string you want to escape.
@return string The escaped string.
*/
function escape_attribute(string $s_string) : string {
return str_replace(
['"'],
['"'],
$s_string,
);
}//end escape_attribute()
/**
When you want that the string appears verbatim inside HTML <pre> tags.
@param string $s_string The string you want to escape.
@return string The escaped string.
*/
function escape_pre(string $s_string) : string {
return str_replace(
[
'&',
'<',
],
[
'&',
'<',
],
$s_string,
);
}//end escape_pre()
}//end namespace string_escaping\HTML
/**
The namespace string_escaping is splitted into subnamespaces for each
language.
This subnamespace is for XHTML (still surprised?)...
@package DevOrSysAdminScripts
@subpackage string_escaping\XHTML
*/
namespace string_escaping\XHTML {
/**
When you want that the string appears as is in XHTML text block.
@param string $s_string The string you want to escape.
@return string The escaped string.
*/
function escape_text(string $s_string) : string {
return str_replace(
[
'&',
'<',
],
[
'&',
'<',
],
$s_string,
);
}//end escape_text()
/**
When you want that the string appears as is in an XHTML tag attribute
that starts and ends with double quotes: my_attribute="something".
Example: a title attribute.
/!\ in double quotes /!\ my_attribute='something' is not supported.
@param string $s_string The string you want to escape.
@return string The escaped string.
*/
function escape_attribute(string $s_string) : string {
return str_replace(
[
'&',
'<',
'"',
"\n",
],
[
'&',
'<',
'"',
' ',
],
$s_string,
);
}//end escape_attribute()
/**
When you want that the string appears verbatim inside XHTML <pre> tags.
@param string $s_string The string you want to escape.
@return string The escaped string.
*/
function escape_pre(string $s_string) : string {
return str_replace(
[
'&',
'<',
],
[
'&',
'<',
],
$s_string,
);
}//end escape_pre()
}//end namespace string_escaping\XHTML
/**
The namespace string_escaping is splitted into subnamespaces for each
language.
This subnamespace is for JS (you're hopeless?)...
@package DevOrSysAdminScripts
@subpackage string_escaping\JS
*/
namespace string_escaping\JS {
use function string_escaping\XHTML\escape_text as XHTML_escape_text;
/**
When you want that the string can be put in a JS string definition.
@param string $s_string The string you want to escape.
@return string The escaped string.
*/
function escape(string $s_string) : string {
return str_replace(
[
'\\',
"\n",
"'",
'"',
],
[
'\\\\',
'\n',
'\\\'',
'\\"',
],
$s_string,
);
}//end escape()
/**
When you want that the string can be put in a JS string definition
to be included inside an XHTML file <script> tag.
@param string $s_string The string you want to escape.
@return string The escaped string.
*/
function escape_for_string_definition_inside_XHTML(string $s_string)
: string {
return XHTML_escape_text(escape($s_string));
}//end escape_for_string_definition_inside_XHTML()
}//end namespace string_escaping\JS
/**
The namespace string_escaping is splitted into subnamespaces for each
language.
This subnamespace is for TeX.
@package DevOrSysAdminScripts
@subpackage string_escaping\TeX
*/
namespace string_escaping\TeX {
/**
When you want that the string can be written to a TeX file inside a
part where TeX is in text mode.
@param string $s_string The string you want to escape.
@return string The escaped string.
*/
function escape_text(string $s_string) : string {
// https://tex.stackexchange.com/a/34586/270015
return str_replace(
[
'\\',
'&',
'%',
'$',
'#',
'_',
'{',
'}',
'~',
'^',
],
[
'\\textbackslash',
'\\&',
'\\%',
'\\$',
'\\#',
'\\_',
'\\{',
'\\}',
'\\textasciitilde',
'\\textasciicircum',
],
$s_string,
);
}//end escape_text()
}//end namespace string_escaping\TeX
/*
$ php
<?php
require_once('string_escaping.libr.php');
var_dump(string_escaping\HTML\escape_text('<&"\'truc\\'));
var_dump(string_escaping\HTML\escape_attribute("<&\"\n'truc\\"));
var_dump(string_escaping\HTML\escape_pre("<&\"\n'truc\\"));
var_dump(string_escaping\XHTML\escape_text('<&"\'truc\\'));
var_dump(string_escaping\XHTML\escape_attribute("<&\"\n'truc\\"));
var_dump(string_escaping\XHTML\escape_pre("<&\"\n'truc\\"));
var_dump(string_escaping\JS\escape("abcd & < > \\ \n \"'"));
var_dump(string_escaping\JS\escape_for_string_definition_inside_XHTML(
"abcd & < > \\ \n \"'"
));
*/
?>