-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHelper.php
More file actions
46 lines (38 loc) · 1.13 KB
/
Helper.php
File metadata and controls
46 lines (38 loc) · 1.13 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
<?php
// Import additionnal class into the global namespace
use \LaswitchTech\Core\Abstracts\Helper;
class DocumentsHelper extends Helper {
/**
* Retrieve Document's Variables
*
* @param string $string
* @return array
*/
public function vars(string $string): array
{
// Regular expression to match the variables in the format %VAR%
$pattern = '/{{([^}]+)}}/';
// Find all matches
preg_match_all($pattern, $string, $matches);
// The variables are in the second element of the $matches array
$variables = array_unique($matches[0]);
// Return the variables
return $variables;
}
/**
* Replace Variables from an array
*
* @param string $string
* @param array $values
* @return string
*/
public function replace(string $string, array $values): string
{
// Replace the placeholders with the corresponding data
foreach ($values as $key => $value) {
$string = str_replace('{{' . $key . '}}', $value ?? '', $string);
}
// Return the string
return $string;
}
}