0

私のユーザーはCMSを使用して求人を入力します。これらの求人では、電子メールアドレスがプレーン形式(please contact job@job.com)またはhtml mailto:リンク(<a href="mailto:job@job.com">jobline</a>およびさらに厄介なもの<a href="mailto:job@job.com">job@job.com</a>)である場合があります。

どちらの形式も検出し、人間に何をすべきかを指示するhtml文字列を作成してスパム防止にするphp関数を作成し、javascriptを使用して、javascript対応のセットアップ用の適切なクリック可能なmailto:linkを再構築したいと思います。私が問題を抱えているのは検出部分です。

以下は、プレーンな電子メールに最適です。mailto:リンクも検出するように適応させるにはどうすればよいですか?

$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 title="$5" class="pep-email">$1(' . $opt_val . ')$2.$3</span>', $the_addrs[$a]);
 }
 $cc = str_replace($the_addrs, $repaddr, $content);

PS:これは既存のワードプレスプラグインを改善するためのものです:PixelineのEメールプロテクター。受賞者の作者は、プラグインコード、説明、および変更ログにクレジットされます。

4

2 に答える 2

1

実際のリンクを取得するには、domdocumentクラスを使用することをお勧めします。これは、さまざまな方法でリンクを記述できるためです。また、正規表現とともに使用して、コンテンツ全体をスキャンし、同時にテキストを置き換えることもできます。

    // The content
$content = 'The stuff from the page';

// Start the dom object
$dom = new DOMDocument();
$dom->recover = true;
$dom->substituteEntities = true;

// Feed the content to the dom object
$dom->loadHTML($content);

// Check each link
foreach ($dom->getElementsByTagName('a') as $anchor) {
// Get the href
$href = $anchor->getAttribute('href');
// Check if it's a mailto link
if (substr($href, 0, 7) == 'mailto:') {
    # Do something with it
    $href = 'new link href';
}
// Put it back in the link
$anchor->setAttribute('href', $href);
}

// Replace the content with the new content
$content = $dom->saveHTML();
于 2013-12-06T20:12:26.290 に答える
0
(<a href="mailto:|)([a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4})(">.+?</a>|)

これはすべてのバリエーションと一致する必要があり、$2に置き換えます

于 2011-02-21T17:32:32.157 に答える