正则表达式 - preg_replace 在我的 PHP 脚本中不起作用
regex - preg_replace in my PHP script doesn't work
我有一个表格,我从 TinyMCE 编辑器中获取 HTML 代码作为我的一个文本区域的输入来制作新闻稿。我想在每个 link 的末尾添加 utm 代码。我模拟了一个 RegEX 模式并替换为在线编辑器,但保存的代码是没有 utm 代码的常用 href。
<?php
$subject=$_POST['subject'];
$from_email=$_POST['from_email'];
$from_name=$_POST['from_name'];
$replyto_email=$_POST['replyto_email'];
$replyto_name=$_POST['replyto_name'];
$lingua=$_POST['lingua'];
$body = mysqli_real_escape_string($db, $_POST['editor1']) ;
$string = $body;
$pattern = '/href="([^"]+)/';
$replacement = '[=12=]?utm_source=newsletter&utm_medium=email&utm_campaign='.$subject;
$txt = preg_replace($pattern, $replacement, $string);
$stmt=null;
$stmt=$db->prepare("INSERT INTO newsletter_email(subject,from_email,from_name,replyto_email,replyto_name,lingua,body) VALUES('$subject','$from_email','$from_name','$replyto_email','$replyto_name','$lingua','$txt')");
if (!$stmt) {
log_msg($db->error);
die();
}
$stmt->execute();
$stmt->close();
$db->close();
?>
来自 TinyMCE 的 $body 内容示例
$body = "<html><head></head><body><a href="example.com" target="_blank">Test</a></body></html>"
preg_replace
之后的$body内容示例
$body = "<html><head></head><body><a href="example.com?utm_source=newsletter&utm_medium=email&utm_campaign=Email Subject" target="_blank">Test</a></body></html>"
最简单的方法是用 [=12=]
代替 </code>:</p>
<pre><code>$subject = 'Email subject';
$string = '<a href="example.com">';
$pattern = '/href="([^"]+)/';
$replacement = '[=10=]?utm_source=newsletter&utm_medium=email&utm_campaign='.$subject;
$txt = preg_replace($pattern, $replacement, $string);
echo $txt;
输出:
<a href="example.com?utm_source=newsletter&utm_medium=email&utm_campaign=Email subject">
使用
mysqli_real_escape_string($db, $_POST['editor1'])
它在标签之间添加了'\',但模式不匹配。改了(注意'.')
$pattern = '/href=."([^"]+)/';
我有一个表格,我从 TinyMCE 编辑器中获取 HTML 代码作为我的一个文本区域的输入来制作新闻稿。我想在每个 link 的末尾添加 utm 代码。我模拟了一个 RegEX 模式并替换为在线编辑器,但保存的代码是没有 utm 代码的常用 href。
<?php
$subject=$_POST['subject'];
$from_email=$_POST['from_email'];
$from_name=$_POST['from_name'];
$replyto_email=$_POST['replyto_email'];
$replyto_name=$_POST['replyto_name'];
$lingua=$_POST['lingua'];
$body = mysqli_real_escape_string($db, $_POST['editor1']) ;
$string = $body;
$pattern = '/href="([^"]+)/';
$replacement = '[=12=]?utm_source=newsletter&utm_medium=email&utm_campaign='.$subject;
$txt = preg_replace($pattern, $replacement, $string);
$stmt=null;
$stmt=$db->prepare("INSERT INTO newsletter_email(subject,from_email,from_name,replyto_email,replyto_name,lingua,body) VALUES('$subject','$from_email','$from_name','$replyto_email','$replyto_name','$lingua','$txt')");
if (!$stmt) {
log_msg($db->error);
die();
}
$stmt->execute();
$stmt->close();
$db->close();
?>
来自 TinyMCE 的 $body 内容示例
$body = "<html><head></head><body><a href="example.com" target="_blank">Test</a></body></html>"
preg_replace
之后的$body内容示例$body = "<html><head></head><body><a href="example.com?utm_source=newsletter&utm_medium=email&utm_campaign=Email Subject" target="_blank">Test</a></body></html>"
最简单的方法是用 [=12=]
代替 </code>:</p>
<pre><code>$subject = 'Email subject';
$string = '<a href="example.com">';
$pattern = '/href="([^"]+)/';
$replacement = '[=10=]?utm_source=newsletter&utm_medium=email&utm_campaign='.$subject;
$txt = preg_replace($pattern, $replacement, $string);
echo $txt;
输出:
<a href="example.com?utm_source=newsletter&utm_medium=email&utm_campaign=Email subject">
使用
mysqli_real_escape_string($db, $_POST['editor1'])
它在标签之间添加了'\',但模式不匹配。改了(注意'.')
$pattern = '/href=."([^"]+)/';