473,396 Members | 1,998 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Regex to Extract an Email Address

24
Hi all,

Please can someone help me! I have experimented on this for hours but I am new to regex! Here is my problem:

I want to get the email address admin@example.net out of this string and store it in $email.

The string is:
<a href='mailto:admin@example.net'>admin@example.net</a>

Please can someone give me code to get the email address out of this string and place it in $email.

All help will be greatly appreciated!
Aug 10 '08 #1
4 5630
Gulzor
27
Hi,

I use the following regular expression to extract emails from files :

^([_a-z0-9-]+)(\.[_a-z0-9-]+)*@([a-z0-9-]+)(\.[a-z0-9-]+)*(\.[a-z]{2,4})$

source : http://www.developer.com/lang/php/article.php/3290141
Aug 11 '08 #2
miraan
24
hmmm,
can you give me the function for that. I am new to regex and i cant seem to use that expression with ereg_replace. Here is what i have tried to do:

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $string="<a href='mailto:admin@example.net'>admin@example.net</a>";
  3. $string=ereg_replace("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$",$1,$string);
  4. echo $string;
  5. ?>
I am not even sure if in ereg_replace you are allowed to use regex as a string pattern or if you can use $1 to use the first match to replace $string with (i.e. the email address found in the string.
Aug 11 '08 #3
miraan
24
Sorry, heres some improved code:

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $string="<a href='mailto:admin@example.net'>admin@example.net</a>";
  3. if(ereg("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$", $string, $regs)) {
  4.       $string=$regs[1];
  5. } else {
  6. echo "No matches!<br>";
  7. echo $string;
  8. }
  9. ?>
  10.  
Aug 11 '08 #4
Gulzor
27
I wrote that script to extract all email addresses contained into a file : (don't forget to replace page.html with your file)

[php]
<?php

$buffer=file_get_contents('page.html');

/* pas de ^ et $ car plusieurs emails par lignes !*/
$regex='`([_a-z0-9-]+)(\.[_a-z0-9-]+)*@([a-z0-9-]+)(\.[a-z0-9-]+)*(\.[a-z]{2,4})`';


$matches=array();
preg_match_all($regex, $buffer, $matches);

$emails=array_unique($matches[0]);

sort($emails, SORT_STRING);


foreach($emails as $email){
echo "$email\r\n";
}


//print_r($emails);

?>
[/php]
Aug 12 '08 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Adrian | last post by:
Hi, I would like to ask whether do anyone who where or how can i extract email address from Exchange Server or Microsoft Outlook from a Web Application Adrian
117
by: Steevo | last post by:
Any suggestions as to the best programs for cloaking email addresses? Many thanks -- Steevo
1
by: Henrik Nyberg | last post by:
Here's a small method for validating email in C#. It may save you some time.. public static bool IsValidEmailAddress(string sEmail) { if (sEmail == null) { return false; } int nFirstAT =...
1
by: Jim Dornbush | last post by:
Has anyone seen an updated regex expression from Microsoft for the email validation expression so that single quotes are allowed? I've been using the canned regex for emails, but recently been...
4
by: Alexander Vasilevsky | last post by:
How to extract email address from the letter in Outlook Express? http://www.alvas.net - Audio tools for C# and VB.Net developers
0
by: Alexander Vasilevsky | last post by:
How to extract email address from the address book in Outlook Express? http://www.alvas.net - Audio tools for C# and VB.Net developers
2
by: Alexander Vasilevsky | last post by:
I need extract email address from the address book in Outlook Express also. http://www.alvas.net - Audio tools for C# and VB.Net developers
1
by: Alexander Vasilevsky | last post by:
How extract email address from the address book in Outlook Express? http://www.alvas.net - Audio tools for C# and VB.Net developers
2
by: Keith G Hicks | last post by:
I'm using the following code to send out email messages to a list of people in a database. My problem is that if I'm sending to 100 people and the 40th address is bad, it crashes on that one and...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.