1

根据我在这里回答的一个问题(使用 PHP 将 HTML 替换为 HTML),我希望能够过滤我的电子邮件地址的输出文本,并将这些文本电子邮件转换为“mailto”链接。

这是有效的 PHP 代码,但仅用于将某些 HTML 转换为其他 HTML。我试图做的是让这个函数查找一个电子邮件地址,并将其转换为“mailto”链接。无论出于何种原因,代码都不会转换电子邮件地址。这是我的PHP:

function text_filter($string) {
    $search  = array('<p>__</p>',   '/[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}/');
    $replace = array('<hr />',      '<a href="mailto:$2">$2</a>');
    $processed_string = str_replace($search, $replace, $string);
    echo $processed_string;
}

当我使用此函数进行输出时,代码如下所示:

<?php text_filter( get_the_content() ); ?>
4

6 回答 6

2
  1. str_replace()不使用正则表达式,用preg_replace().
  2. 为第一个匹配表达式添加了分隔符。
  3. 固定替换从$1$2

 

function text_filter($string) {
    $search  = array('/<p>__<\/p>/', '/([a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4})/');
    $replace = array('<hr />', '<a href="mailto:$1">$1</a>');
    $processed_string = preg_replace($search, $replace, $string);
    echo $processed_string;
}
于 2013-04-10T14:58:38.937 回答
1

@Adam Baney - 即使重复使用,这也会起作用。

// EMAILS
$str =  preg_replace('~(^|[\s\.,;\n\(])([a-zA-Z0-9._+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4})~',
  '$1<a href="mailto:$2">$2</a>',
  $str);

// PHONE NUMBERS
$str =  preg_replace_callback('~(^|[\s\.,;\n\(])(?<! )([0-9 \+\(\)]{9,})~', function($m) {
  return $m[1].'<a href="tel:'.preg_replace('~[^0-9\+]~', '', $m[2]).'">'.$m[2].'</a>';
}, $str);

于 2019-11-25T10:15:16.107 回答
0

您不能使用str_replace正则表达式替换。

您将需要拆分操作。

function text_filter($string) {
    $search  = array('<p>__</p>');
    $replace = array('<hr />');
    $processed_string = str_replace($search, $replace, $string);
$processed_string = preg_replace('/[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}/','<a href="mailto:$2">$2</a>',$processed_string);
    echo $processed_string;
}

请参阅:http : //www.php.net/manual/en/function.preg-replace.php 更换预浸料。

于 2013-04-10T14:57:12.743 回答
0
function obfuscate_email($content){

    $pattern = '#([0-9a-z]([-_.]?[0-9a-z])*@[0-9a-z]([-.]?[0-9a-z])*\\.';
    $pattern .= '[a-wyz][a-z](fo|g|l|m|mes|o|op|pa|ro|seum|t|u|v|z)?)#i';
    $replacement = '<a href="mailto:\\1">\\1</a>';
    $content = preg_replace($pattern, $replacement, $content);
    return $content;

}

并添加过滤器

add_filter( 'the_content', 'obfuscate_email' );
于 2014-04-09T00:59:19.587 回答
0

另一种方法是这样做,以便它可以与文本中现有的 html 链接一起使用:

function html_parse_text($text)
{    
    $text = preg_replace("/(?<!\")(((f|ht){1}tps?:\/\/)[-a-zA-Z0-9@:%_\+.~#?&\/\/=]+)/",
            '<a href="\\1" target=_blank>\\1</a>', $text);       
    $text = preg_replace("/([[:space:]()[{}])(www.[-a-zA-Z0-9@:%_\+.~#?&\/\/=]+)/",
            '\\1<a href="http://\\2" target=_blank>\\2</a>', $text);
    $text = preg_replace("/(?<!\")([_\.0-9a-z-]+@([0-9a-z][0-9a-z-]+\.)+[a-z]{2,3})/",
            '<a href="mailto:\\1" target=_blank>\\1</a>', $text);

    return $text;
}
于 2014-05-22T21:49:13.753 回答
0

这是另一个似乎对我有用的版本。我添加了 + char 来处理“加地址”(比如 some+email@address.com)

function replaceemail($text) {-
    $ex = "/([a-zA-Z0-9._+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4})/";
    preg_match_all($ex, $text, $url);
    foreach($url[0] as $k=>$v) $text = str_replace($url[0][$k], '<a href="mailto:'.$url[0][$k].'" target="_blank" rel="nofollow">'.$url[0][$k].'</a>', $text);
    return $text;
}
于 2014-07-02T16:24:46.457 回答