Source Code: global_functions_inc.php

 1: <?php
 2: /**
 3:  * This file contains the implementation of the auxiliary global fuctions.
 4:  *
 5:  * @package Utility Functions
 6:  *
 7:  * @author Oleg Schildt
 8:  */
 9: 
10: /**
11:  * Function for escaping the special symbols for putting into the regexp pattern 
12:  * as a simple text in the pattern.
13:  *
14:  * This function escapes all special symbols so that they are treated as a simple text.
15:  *
16:  * @param string $str
17:  * The string to be escaped.
18:  *
19:  * @param string $delimiter
20:  * The string used in the pattern.
21:  *
22:  * @return string
23:  * The escaped string.
24:  *
25:  * @see \preg_replacement_quote()
26:  *
27:  * @author Oleg Schildt
28:  */
29: function preg_pattern_quote(string $str, string $delimiter = "/"): string
30: {
31:     return preg_quote($str, $delimiter);
32: }
33: 
34: /**
35:  * Function for escaping the special symbols for putting into the regexp pattern 
36:  * as a simple text.
37:  *
38:  * This function escapes all special symbols so that they are treated as a 
39:  * simple text in the replacement.
40:  *
41:  * @param string $str
42:  * The string to be escaped.
43:  *
44:  * @return string
45:  * The escaped string.
46:  *
47:  * @see \preg_pattern_quote()
48:  *
49:  * @author Oleg Schildt
50:  */
51: function preg_replacement_quote(string $str): string
52: {
53:     return strtr($str, ["\\" => "\\\\", "$" => "\\$"]);
54: }
55: ?>