Foreground: I have this project I'm working on which uses email templates stored in database, each email body contain several shortcodes in the pattern {{$shortcode}}
.
Problem: Currently there is an admin setting for changing email template body. During edit, I want to add a Hint Notice to the person making the edit displaying the allowed shortcodes.
E.g You are editing XYZ template, the allow shortcodes are {{$shortcode1}}, {{$shortcode2}}, and {{$shortcode3}}
What I Want to do: The website has several email template with varying shortcodes, and the templates are prone to be changed or new once added.
To avoid going to each template and listing out each shortcode it has, In the controller, I want to check for the shortcodes in the template, extract them and pass them into the view.
E.g
$message = 'Hello {{$first_name}}, you have requested to change your password, your reset link is {{$link}}';
$shortcodes = explode(' ', $message);
if I dd($shortcodes)
, I get this array
0 => "Hello"
1 => "{{$first_name}},"
2 => "you"
3 => "have"
4 => "requested"
5 => "to"
6 => "change"
7 => "your"
8 => "password,"
9 => "your"
10 => "reset"
11 => "link"
12 => "is"
13 => "{{$link}}"
Now I want to get all the words starting with {{$
in the above array, is this possible ?
CodePudding user response:
Maybe you should use regular expressions here, like this:
$message = 'Hello {{$first_name}}, you have requested to change your password, your reset link is {{$link}}';
preg_match_all('/{{\$\w }}/', $message, $matches);
dd($matches[0]);
/*
[
0 => "{{$first_name}}"
1 => "{{$link}}"
]
*/
CodePudding user response:
You can use array_filter
:
$result = array_filter($words, fn($word) => strpos($word, '{{$') === 0);
But perhaps using a regular expression on the email template's text would be a cleaner solution (e.g. with preg_match_all
: https://www.php.net/manual/en/function.preg-match-all.php)
CodePudding user response:
$message = 'Hello {{$first_name}}, you have requested to change your password, your reset link is {{$link}}';
$shortcodes = explode(' ', $message);
foreach($shortcodes as $sc){
if( ($sc[0] == "{") && ($sc[1] == "{") && ($sc[2] == "$") ){
echo $sc;
}
}
CodePudding user response:
Why no one uses the function made for this?
foreach($shortcodes as $word){
if(str_starts_with($word, 'x')){
## CODE TO BE EXECUTED
}
}
CodePudding user response:
Please use this script:
<?php
function getShortCodes($string, $start, $end){
$count = substr_count($string, $start);
if ($count == 0) return [];
$matchings = [];
for($i = 0; $i <= $count; $i ){
$string = ' ' . $string;
$ini = strpos($string, $start);
if ($ini == 0) continue;
$ini = strlen($start);
$len = strpos($string, $end, $ini) - $ini;
$subStr = substr($string, $ini, $len);
$matchings[] = $subStr;
$string = str_replace('{{$'.$subStr.'}}',"",$string);
}
return $matchings;
}
$message = 'Hello {{$first_name}}, you have requested to change your password, your reset link is {{$link}}';
$parsed = getShortCodes($message, '{{$', '}}');
print_r($parsed);
CodePudding user response:
An easier solution would be to use preg_match_all()
$message = 'Hello {{$first_name}}, you have requested to change your password, your reset link is {{$link}}';
preg_match_all('/{{[^{]*}}/', $message, $matches);
var_dump($matches);
//array(1) {
// [0]=>
// array(2) {
// [0]=>
// string(15) "{{$first_name}}"
// [1]=>
// string(9) "{{$link}}"
// }
//}