DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Beyond Extensions: Architectural Deep-Dives into File Upload Security
  • How Laravel Developers Handle Database Migrations Without Downtime
  • Migrating from Monolith to Microservices Using PHP: A Step-by-Step Guide
  • Inheritance in PHP: A Simple Guide With Examples

Trending

  • Jakarta NoSQL: Why JPA Is Not Enough for the AI Era
  • Cutting Data Pipeline Costs and Data Freshness Issues With Netflix Maestro and Apache Iceberg: A Practical Tutorial
  • The Latency Tax That’s Hidden in Cloud-Native Systems (and the Hard Lessons I Learned to Minimize It)
  • How to Set MX Records via API: Automate Email Routing Programmatically
  1. DZone
  2. Coding
  3. Languages
  4. Encode Email Addresses With PHP

Encode Email Addresses With PHP

By 
Paul Underwood user avatar
Paul Underwood
·
Mar. 20, 13 · Interview
Likes (0)
Comment
Save
Tweet
Share
10.2K Views

Join the DZone community and get the full member experience.

Join For Free

Spam bots will crawl your pages exactly the same way Search Engines crawls your pages, but while Search Engines are crawling to index your content, spam bots are crawling to find certain information.

One of the most common bits of data they are searching for are email addresses, this is because they can store these email addresses in a list and send out spam emails.

Displaying your email address on your web page makes it very easy for your visitors to store this and send you email, but you will need to get around the spam bot problem to do this you will need to HTML encode your email address.

While encoding your email address will make it display correctly in the browser in the source code the email address will be encoded so that spam bots can not read the email.

Below is a PHP code snippet which will allow you to pass in an email address and encode it so that you can display the email address on your website. This uses the PHP function ord() to return an ASCII value for the character used in the email.

/**
 * Encode an email address to display on your website
 */
function encode_email_address( $email ) {
     $output = '';
     for ($i = 0; $i < strlen($email); $i++)
     {
          $output .= '&#'.ord($email[$i]).';';
     }
     return $output;
}

To use in on your page all you have to do is pass in a parameter and it will return the encoded email you can now use on your website.

$encodedEmail = encode_email_address( '[email protected]' );
printf('<a href="mailto:%s">%s</a>', $encodedEmail, $encodedEmail);




PHP

Published at DZone with permission of Paul Underwood. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Beyond Extensions: Architectural Deep-Dives into File Upload Security
  • How Laravel Developers Handle Database Migrations Without Downtime
  • Migrating from Monolith to Microservices Using PHP: A Step-by-Step Guide
  • Inheritance in PHP: A Simple Guide With Examples

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook