preg_replace 特殊字符问题
preg_replace issue with special characters
我正在尝试对某些变量进行 preg_replace。我的代码如下:
$message=Welcome, %%name%%!
Congratulations! You are now a member of Expert Online Training (EOT), the world’s best virtual classroom for youth development professionals. By using EOT now, before your job starts at %%campname%%, you will turbocharge your leadership skills, boost your self-confidence, and get even more out of %%campname%%’s on-site training.
EOT Logo Take EOT with you. We know you are busy, so our new website is mobile-friendly. You can now watch EOT videos and take your quizzes on any smartphone, tablet, or laptop with a WiFi connection. Imagine learning more about behavior management, leadership, supervision, games, and safety while you sit in a café, library, or student lounge!
%%directorname%% just created an account for you with these login credentials:
%%logininfo%%
To watch EOT’s intro vid and log in, click here.
When is it due? Directors usually require staff to complete their online learning assignment before arriving on-site. If you have not yet received a due-date for your assignment, check with %%directorname%% to get one. As you move through your course, %%directorname%% will have access to an electronic dashboard that allows them to track your progress and quiz scores.
$loginInfo = 'Username: ' . $email . '<br/>'; // Login Information
$loginInfo .= 'Password: ' . $password;
$name = $first_name . ' ' . $last_name;
$campname = get_the_title($org_id); // the camp name
$vars = array(
'name' => $name,
'email' => $email,
'your_name' => $directorname,
'logininfo' => $loginInfo,
'directorname' => $directorname,
'campname' => $campname,
'numvideos' => NUM_VIDEOS,
);
/* Replace %%VARIABLE%% using vars*/
foreach($vars as $key => $value)
{
$message = preg_replace('/%%' . $key . '%%/', $value, $message);
}
echo $message; //send message
$email、$password、$first_name、$last_name、$loginInfo、$directorname 和 $campname 都可以在脚本中使用。除了包含“$”后跟数字的密码外,所有内容都被正确替换,例如像 "maner" 这样的密码被替换为 "maner"。密码中的“$1”或“$2”被删除。如果我使用的安全密码可能包含特殊字符和数字,如 "Ch33tos" 不会被剥离为 "Ch33tos"
,我如何确保消息被正确的密码替换
对于这种基本的东西,你最好使用 str_replace:http://php.net/str_replace
示例:
/* Replace %%VARIABLE%% using vars*/
foreach($vars as $key => $value)
{
$message = str_replace('%%' . $key . '%%', $value, $message);
}
这应该可以防止密码中的特殊字符出现任何问题。
请注意,您的问题没有具体说明,但如果您通过电子邮件发送此消息,您应该不在电子邮件中发送密码。电子邮件不是安全的媒介。
实际上甚至不需要循环数组。
Str_replace 接受数组。
在数组中每个键的开头和结尾添加 %%
,此代码将全部替换它们。
$vars = array(
'%%name%%' => $name,
'%%email%%' => $email,
'%%your_name%%' => $directorname,
'%%logininfo%%' => $loginInfo,
'%%directorname%%' => $directorname,
'%%campname%%' => $campname,
'%%numvideos%%' => NUM_VIDEOS);
$message = str_replace(array_keys($vars) , $vars , $message);
请在此处查看工作示例 https://3v4l.org/T8YGs
如果您仍想使用 preg_replace,则需要转义字符串中的 $
。
有关示例,请参见此 regex101,https://regex101.com/r/BRJDCV/1
我正在尝试对某些变量进行 preg_replace。我的代码如下:
$message=Welcome, %%name%%!
Congratulations! You are now a member of Expert Online Training (EOT), the world’s best virtual classroom for youth development professionals. By using EOT now, before your job starts at %%campname%%, you will turbocharge your leadership skills, boost your self-confidence, and get even more out of %%campname%%’s on-site training. EOT Logo Take EOT with you. We know you are busy, so our new website is mobile-friendly. You can now watch EOT videos and take your quizzes on any smartphone, tablet, or laptop with a WiFi connection. Imagine learning more about behavior management, leadership, supervision, games, and safety while you sit in a café, library, or student lounge!
%%directorname%% just created an account for you with these login credentials:
%%logininfo%%
To watch EOT’s intro vid and log in, click here.
When is it due? Directors usually require staff to complete their online learning assignment before arriving on-site. If you have not yet received a due-date for your assignment, check with %%directorname%% to get one. As you move through your course, %%directorname%% will have access to an electronic dashboard that allows them to track your progress and quiz scores.
$loginInfo = 'Username: ' . $email . '<br/>'; // Login Information
$loginInfo .= 'Password: ' . $password;
$name = $first_name . ' ' . $last_name;
$campname = get_the_title($org_id); // the camp name
$vars = array(
'name' => $name,
'email' => $email,
'your_name' => $directorname,
'logininfo' => $loginInfo,
'directorname' => $directorname,
'campname' => $campname,
'numvideos' => NUM_VIDEOS,
);
/* Replace %%VARIABLE%% using vars*/
foreach($vars as $key => $value)
{
$message = preg_replace('/%%' . $key . '%%/', $value, $message);
}
echo $message; //send message
$email、$password、$first_name、$last_name、$loginInfo、$directorname 和 $campname 都可以在脚本中使用。除了包含“$”后跟数字的密码外,所有内容都被正确替换,例如像 "maner" 这样的密码被替换为 "maner"。密码中的“$1”或“$2”被删除。如果我使用的安全密码可能包含特殊字符和数字,如 "Ch33tos" 不会被剥离为 "Ch33tos"
,我如何确保消息被正确的密码替换对于这种基本的东西,你最好使用 str_replace:http://php.net/str_replace
示例:
/* Replace %%VARIABLE%% using vars*/
foreach($vars as $key => $value)
{
$message = str_replace('%%' . $key . '%%', $value, $message);
}
这应该可以防止密码中的特殊字符出现任何问题。
请注意,您的问题没有具体说明,但如果您通过电子邮件发送此消息,您应该不在电子邮件中发送密码。电子邮件不是安全的媒介。
实际上甚至不需要循环数组。
Str_replace 接受数组。
在数组中每个键的开头和结尾添加 %%
,此代码将全部替换它们。
$vars = array(
'%%name%%' => $name,
'%%email%%' => $email,
'%%your_name%%' => $directorname,
'%%logininfo%%' => $loginInfo,
'%%directorname%%' => $directorname,
'%%campname%%' => $campname,
'%%numvideos%%' => NUM_VIDEOS);
$message = str_replace(array_keys($vars) , $vars , $message);
请在此处查看工作示例 https://3v4l.org/T8YGs
如果您仍想使用 preg_replace,则需要转义字符串中的
$
。有关示例,请参见此 regex101,https://regex101.com/r/BRJDCV/1