我想用电子邮件的图像替换字符串中的所有电子邮件。
我已经有了一个PHP函数来创建提供给它的文本的图像。所以我只是在寻找如何用相应的basse64engoded字符串替换电子邮件。
这就是我想要的:
"my email is example@example.com and my phone no is 349080353"
我想要一个函数将上面的字符串转换成以下内容:
my email is <img src="image.php?id=ZG5zLWFkbWluQ437yifhb2dsZS5jb20="> and my phone no is 349080353
电子邮件的ID使用base64_encode
进行编码。因此,我想要一个函数来搜索和替换带有以下img标记的电子邮件,并使用base64_encode
对每封电子邮件进行编码,并将其提供给“id”。
答案 0 :(得分:2)
试试此代码
$email_pattern = '/[a-z0-9!#$%&\'*+\/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&\'*+\/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?/';
$html = preg_replace_callback($email_pattern, "encode_email", "my email is example@example.com and my phone no is 349080353");
echo $html;
function encode_email($matches){
return '<img src="image.php?id='. base64_encode($matches[0]) .'">';
}
答案 1 :(得分:0)
$content = "my email is example@example.com and my phone no is 349080353"
preg_match("/[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})/i", $content, $matches);
print $matches[0];
答案 2 :(得分:0)
检查这个有用:)
<?php
$string = "my email is example@example.com and my phone no is 349080353";
$pattern = "/[^@\s]*@[^@\s]*\.[^@\s]*/";
$replacement = '<img src="">';
$string1 = preg_replace($pattern, $replacement, $string);
echo $string1
?>
答案 3 :(得分:0)
<?php
$content = "my email is example@example.com, my 2nd email is example2@example.com and my phone no is 349080353";
$c='a-zA-Z-_0-9'; // allowed characters in domainpart
$la=preg_quote('!#$%&\'*+-/=?^_`{|}~', "/"); // additional allowed in first part (localpart)
$email="[$c$la][$c$la\.]*[^.]@[$c]+\.[$c]+";
preg_match_all("/\b$e\b/", $content, $matches);
foreach ($matches[0] as $e){
$content = preg_replace("/\b($email)\b/", '<img src="'.base64_encode($e).'">', $content);
}
echo $content;
要捕获所有有效的电子邮件地址,请参阅:What characters are allowed in an email address?