Jump to content


Photo

extracting an email address from string


  • Please log in to reply
6 replies to this topic

#1 ozPATT

ozPATT
  • Members
  • PipPip
  • Member
  • 15 posts

Posted 26 July 2006 - 10:32 AM

Hi All,

I have a CMS, where people enter information into a textarea, that gets inserted into a db. What I would like to do, is replace all email addresses, with a function that will mask the address, but to do that, i need to find a way to find the address to replace.

I can find the @ easy enough, but I don't know how to get the whole address.

Any help would be good.

Thanks

Patrick

#2 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 26 July 2006 - 10:36 AM

This is a valadation email code you can use it to convert to what you want ok.
if(!eregi("^[a-z0-9_]+@[a-z0-9\-]+\.[a-z0-9\-\.]+$" ,$email)) {

echo"please add a correct email address<br><a href='what_ever.php'>Please try agin!</a>";
exit;

Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc

#3 ozPATT

ozPATT
  • Members
  • PipPip
  • Member
  • 15 posts

Posted 26 July 2006 - 10:52 AM

sorry, must not have explained properly...

I need to mask email addresses. So I have a function, that takes 2 arguments, the user and the domain.

For every email address in a given text, i need to replace it with say, MaskEmail($email) - $email being the address...

#4 Kris

Kris
  • Staff Alumni
  • Advanced Member
  • 2,755 posts
  • LocationThe Internet

Posted 26 July 2006 - 11:00 AM

You could do something like this:
<?php
function mask_email($email) {
    $patterns = array("/\./","/@/");
    $replacements = array(" [dot] "," [at] ");
    return preg_replace($patterns, $replacements, $email);
}
echo mask_email("mail@somewhere.com");
?>
EDIT: I just re-read your post, and you want to know how to find the email address in a block of text, right? How about this:
<?php
$str = "Hello, my email address is mail@somewhere.com what is your email address? Is it somewhere@mail.com ?";
preg_match_all("/[a-z0-9_]+@[a-z0-9\-]+\.[a-z0-9\-\.]+/",$str,$out);

foreach($out[0] as $email) {
    $str = str_replace($email,mask_email($email),$str);
}
echo $str;
?>

An alternative option would be to use preg_replace() I guess, but I'm not that good at regex to offer a way of using it to the extent that you need to!

#5 ozPATT

ozPATT
  • Members
  • PipPip
  • Member
  • 15 posts

Posted 26 July 2006 - 11:07 AM

haha, thanks again for the reply, but I think  i am not being too clear...

i have the function to mask the email, my problem is actually getting the email address from the text, as there may be several addresses in one section. So each address will need to be replaced, with the function call.

if that makes sense.,

thanks for the prrompt replies though...

#6 Kris

Kris
  • Staff Alumni
  • Advanced Member
  • 2,755 posts
  • LocationThe Internet

Posted 26 July 2006 - 11:12 AM

^^ EDITED MY POST ABOVE ^^

#7 ozPATT

ozPATT
  • Members
  • PipPip
  • Member
  • 15 posts

Posted 26 July 2006 - 11:22 AM

ooh, that looks like it could be just the ticket... i will give that a try and let you know how i get on. thanks for that!

woohoo!! worked! :o)

thanks! Now I just need to work out another scripting problem, but perhaps you could help me with that too if you have time?

My client needs to enter articles onto his site, done the same way. Currently, he includes some javascript that will open up a popup window, and show the user an image, stored in the popup directory. I need to find a way to incorporate this into the site, and I think a similar way would be good.

What I am thinking, is if he was to put: [popup="image.jpg"] wherever he wanted these popups to go, then i just extract where it says the above, and take out what is in the quote marks. then replace, using the extracted as the href src or something like that. does that make sense?

thanks for your help with the above again. :)




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users