PhpRiot
Become Zend Certified

Prepare for the ZCE exam using our quizzes (web or iPad/iPhone). More info...


When you're ready get 7.5% off your exam voucher using voucher CJQNOV23 at the Zend Store

Hiding email addresses

Zend_Service_ReCaptcha_MailHide can be used to hide email addresses. It will replace a part of an email address with a link that opens a popup window with a reCAPTCHA challenge. Solving the challenge will reveal the complete email address.

In order to use this component you will need an account to generate public and private keys for the mailhide API.

Example 843. Using the mail hide component

<?php
// The mail address we want to hide
$mail 'mail@example.com';

// Create an instance of the mailhide component, passing it your public
// and private keys, as well as the mail address you want to hide
$mailHide = new Zend_Service_ReCaptcha_MailHide();
$mailHide->setPublicKey($pubKey);
$mailHide->setPrivateKey($privKey);
$mailHide->setEmail($mail);

// Display it
print($mailHide);

The example above will display "m...@example.com" where "..." has a link that opens up a popup window with a reCAPTCHA challenge.

The public key, private key, and the email address can also be specified in the constructor of the class. A fourth argument also exists that enables you to set some options for the component. The available options are listed in the following table:

Table 156. Zend_Service_ReCaptcha_MailHide options

Option Description Expected Values Default Value
linkTitle The title attribute of the link string 'Reveal this e-mail address'
linkHiddenText The text that includes the popup link string '...'
popupWidth The width of the popup window int 500
popupHeight The height of the popup window int 300


The configuration options can be set by sending them as the fourth argument to the constructor or by calling setOptions($options), which takes an associative array or an instance of Zend_Config.

Example 844. Generating many hidden email addresses

<?php
// Create an instance of the mailhide component, passing it your public
// and private keys, as well as some configuration options
$mailHide = new Zend_Service_ReCaptcha_MailHide();
$mailHide->setPublicKey($pubKey);
$mailHide->setPrivateKey($privKey);
$mailHide->setOptions(array(
    
'linkTitle' => 'Click me',
    
'linkHiddenText' => '+++++',
));

// The mail addresses we want to hide
$mailAddresses = array(
    
'mail@example.com',
    
'johndoe@example.com',
    
'janedoe@example.com',
);

foreach (
$mailAddresses as $mail) {
    
$mailHide->setEmail($mail);
    print(
$mailHide);
}

Zend Framework