Jump to content

extracting an email address from string


ozPATT

Recommended Posts

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
Link to comment
Share on other sites

This is a valadation email code you can use it to convert to what you want ok.
[code]
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;
[/code]
Link to comment
Share on other sites

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...
Link to comment
Share on other sites

You could do something like this:
[code]<?php
function mask_email($email) {
    $patterns = array("/\./","/@/");
    $replacements = array(" [dot] "," [at] ");
    return preg_replace($patterns, $replacements, $email);
}
echo mask_email("mail@somewhere.com");
?>[/code]
[b]EDIT:[/b] I just re-read your post, and you want to know how to [i]find[/i] the email address in a block of text, right? How about this:
[code]<?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;
?>[/code]

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!
Link to comment
Share on other sites

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...
Link to comment
Share on other sites

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. :)
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.