2

I want to replace all emails in a string with an image of the email.

I already have a PHP function to create an image of the text supplied to it. So I am just looking for how to replace the emails with the corresponding basse64engoded string.

Here is what I want exactly:

"my email is example@example.com and my phone no is 349080353"

I want a function to convert the above string into following:

my email is <img src="image.php?id=ZG5zLWFkbWluQ437yifhb2dsZS5jb20="> and my phone no is 349080353

The id of the email is encoded with base64_encode. So, I want a function to search and replace email with the following img tag and encode each email with base64_encode and supply it to 'id'.

rubo77
  • 19,527
  • 31
  • 134
  • 226
John
  • 163
  • 1
  • 11

4 Answers4

2

Try this code

$email_pattern = '/[a-z0-9!#$%&\'*+\/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&\'*+\/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?/';

$html = preg_replace_callback($email_pattern, "encode_email", "my email is example@example.com and my phone no is 349080353");

echo $html;

function encode_email($matches){
    return '<img src="image.php?id='. base64_encode($matches[0]) .'">';
}
Keith
  • 1,352
  • 3
  • 21
  • 48
Arif
  • 978
  • 12
  • 29
0
$content = "my email is example@example.com and my phone no is 349080353"
preg_match("/[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})/i", $content, $matches);

print $matches[0];
rubo77
  • 19,527
  • 31
  • 134
  • 226
Fortran
  • 593
  • 4
  • 14
  • Welcome to Stack Overflow! Please read [what this site is about](https://stackoverflow.com/about) and "[How to answer](https://stackoverflow.com/help/how-to-answer)" before answering a question. – Miroslav Glamuzina May 07 '19 at 13:02
0

Checked this works :)

<?php 
 $string = "my email is example@example.com and my phone no is 349080353";
 $pattern = "/[^@\s]*@[^@\s]*\.[^@\s]*/";
 $replacement = '<img src="">';
 $string1 = preg_replace($pattern, $replacement, $string);
 echo $string1
?>
Dinkar Thakur
  • 3,025
  • 5
  • 23
  • 35
  • Its giving me: my email is and my phone no is 349080353 – John Jan 25 '13 at 13:29
  • you have to use `$replacement = ` that was just dummy code – Dinkar Thakur Jan 25 '13 at 13:29
  • ZG5zLWFkbWluQ437yifhb2dsZS5jb20= is dynamic and not static. if you read my question again, this is the base64_encode value of the email in the string – John Jan 25 '13 at 13:32
  • can you clarify a bit more as from where you get the encoded string so that i can make exact function for that. sorry for bothering you. i just want to try it :) – Dinkar Thakur Jan 25 '13 at 13:35
0
<?php
$content = "my email is example@example.com, my 2nd email is example2@example.com and my phone no is 349080353";
$c='a-zA-Z-_0-9'; // allowed characters in domainpart
$la=preg_quote('!#$%&\'*+-/=?^_`{|}~', "/"); // additional allowed in first part (localpart)
$email="[$c$la][$c$la\.]*[^.]@[$c]+\.[$c]+";
preg_match_all("/\b$e\b/", $content, $matches);
foreach ($matches[0] as $e){
  $content = preg_replace("/\b($email)\b/", '<img src="'.base64_encode($e).'">', $content);
}
echo $content;

To catch really all valid email addresses, see: What characters are allowed in an email address?

rubo77
  • 19,527
  • 31
  • 134
  • 226