Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/266.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 正则表达式:将电子邮件地址替换为_Php_Regex_Email - Fatal编程技术网

Php 正则表达式:将电子邮件地址替换为

Php 正则表达式:将电子邮件地址替换为,php,regex,email,Php,Regex,Email,我已经开发了一个wordpress插件,它可以浏览一堆html并检测任何电子邮件地址,用一个不可获取的html标记(为了更好的可用性,可以通过javascript重新以电子邮件的形式呈现)代替它们 例如,函数接收: $content = "Hello john@doe.com. How are you today?"; $content = "Hello john@doe.com(John Doe). How are you today?"; 和产出: $content = "Hello &

我已经开发了一个wordpress插件,它可以浏览一堆html并检测任何电子邮件地址,用一个不可获取的html标记(为了更好的可用性,可以通过javascript重新以电子邮件的形式呈现)代替它们

例如,函数接收:

$content = "Hello john@doe.com. How are you today?";
$content = "Hello john@doe.com(John Doe). How are you today?";
和产出:

$content = "Hello <span class="email">john(replace this parenthesis by @)example.com</span>. How are you today?";
新的产出将是:

$content = "Hello <span class="email" title="John Doe">john(replace this parenthesis by @)example.com</span>. How are you today?";
$content=“你好,约翰(用@替换括号)example.com。你今天好吗?”;
因此,正则表达式应该查找附加的括号,如果找到,则获取其中的内容并添加html标题属性,删除括号,然后解析电子邮件

我对如何实现这一点几乎一无所知,因为该特性具有可选性(意思是:那些括号并不总是存在)

任何指针都会有帮助,以下是我当前的代码:

function pep_replace_excerpt($content) {
     $addr_pattern = '/([A-Z0-9._%+-]+)@([A-Z0-9.-]+)\.([A-Z]{2,4})/i';
        preg_match_all($addr_pattern, $content, $addresses);
        $the_addrs = $addresses[0];
        for ($a = 0; $a < count($the_addrs); $a++) {
            $repaddr[$a] = preg_replace($addr_pattern, '<span class="email" title="$4">$1(replace this parenthesis by @)$2.$3</span>', $the_addrs[$a]);
        }
        $cc = str_replace($the_addrs, $repaddr, $content);
        return $cc;
}
函数pep\u replace\u摘录($content){
$addr_pattern='/([A-Z0-9.[uz0%+-]+])@([A-Z0-9.-]+)\.([A-Z]{2,4})/i';
preg_match_all($addr_pattern,$content,$address);
$theu addrs=$addresses[0];
对于($a=0;$a
简单的选择可能是在电子邮件发送后立即使用strpos检查括号是否存在,然后使用正则表达式查找电子邮件发送后第一次出现的
((.+?)

另一个选项是将
(.+?)?
添加到您的正则表达式中,最后一个问号将使组成为可选组

然后,傻瓜代码将如下所示:

function pep_replace_excerpt($content) {
     $addr_pattern = '/([A-Z0-9._%+-]+)@([A-Z0-9.-]+)\.([A-Z]{2,4})(\((.+?)\))?/i';
        preg_match_all($addr_pattern, $content, $addresses);
        $the_addrs = $addresses[0];
        for ($a = 0; $a < count($the_addrs); $a++) {
            if(count($the_addrs[$i]) == 4)
                $repaddr[$a] = preg_replace($addr_pattern, '$1(replace this parenthesis by @)$2.$3', $the_addrs[$a]);
            else
                $repaddr[$a] = preg_replace($addr_pattern, '$1(replace this parenthesis by @)$2.$3', $the_addrs[$a]);
        }
        $cc = str_replace($the_addrs, $repaddr, $content);
        return $cc;
}
函数pep\u replace\u摘录($content){
$addr_pattern='/([A-Z0-9.\%+-]+)@([A-Z0-9.-]+)\([A-Z]{2,4})(\((.+)\)?/i';
preg_match_all($addr_pattern,$content,$address);
$theu addrs=$addresses[0];
对于($a=0;$a}
非常好!有一件事困扰着我:4美元,把父母还给他。有没有办法在正则表达式中删除它们?否则,我想我可以通过php函数来修复这个问题。谢谢:)我编辑了regex模式,这样组就只是括号内的文本了。嗯,效果不太好:使用右括号似乎失败了。我会继续努力。。。这里有一个复制/可复制的代码:好的,快速修复(我现在不能检查php,因为我在打电话)-将(.*)更改为(^),我将在解决方案中更改它。我找到了!我会更新你的答案并接受它。谢谢!