Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- $string = file_get_contents("example.txt"); // Load text file contents
- $matches = array(); //create array
- $pattern = '/[A-Za-z0-9_-]+@[A-Za-z0-9_-]+.([A-Za-z0-9_-][A-Za-z0-9_]+)/'; //regex for pattern of e-mail address
- preg_match($pattern, $string, $matches); //find matching pattern
- $string = file_get_contents("example.txt"); // Load text file contents
- // don't need to preassign $matches, it's created dynamically
- // this regex handles more email address formats like a+b@google.com.sg, and the i makes it case insensitive
- $pattern = '/[a-z0-9_-+]+@[a-z0-9-]+.([a-z]{2,3})(?:.[a-z]{2})?/i';
- // preg_match_all returns an associative array
- preg_match_all($pattern, $string, $matches);
- // the data you want is in $matches[0], dump it with var_export() to see it
- var_export($matches[0]);
- array (
- 0 => 'test1+2@gmail.com',
- 1 => 'test-2@yahoo.co.jp',
- 2 => 'test@test.com',
- 3 => 'test@test.co.uk',
- 4 => 'test@google.com.sg',
- )
- /[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+.[A-Za-z]{2,4}/
- $sourceeee= 'Here are examplr mymail@yahoo.com and my-e.mail@goog.com or something more';
- preg_match_all('/[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+.[A-Za-z]{2,4}/i', $sourceeee, $found_mails);
- <?
- $url="http://example.com/";
- $text=file_get_contents($url);
- $res = preg_match_all(
- "/[a-z0-9]+[_a-z0-9.-]*[a-z0-9]+@[a-z0-9-]+(.[a-z0-9-]+)*(.[a-z]{2,4})/i",
- $text,
- $matches
- );
- if ($res) {
- foreach(array_unique($matches[0]) as $email) {
- echo $email . "<br />";
- }
- }
- else {
- echo "No emails found.";
- }
- ?>
- <?php
- $content = "Hi my name is Joe, I can be contacted at joe@mysite.com.";
- preg_match("/[_a-z0-9-]+(.[_a-z0-9-]+)*@[a-z0-9-]+(.[a-z0-9-]+)*(.[a-z]{2,3})/i", $content, $matches);
- print $matches[0];
- ?>
- <?php
- function extract_email_addresses($str){
- $emails = array();
- $str = strip_tags( $str );
- $str = preg_replace('/s+/', ' ', $str);
- $str = preg_replace("/[nr]/", "", $str);
- $remove_chars = array (',', "<", ">", ";", "'", ". ");
- $str = str_replace( $remove_chars, ' ', $str );
- $parts = explode(' ', $str);
- if(count($parts) > 0){
- foreach($parts as $part){
- $part = trim($part);
- if( $part != '' ) {
- if( filter_var($part, FILTER_VALIDATE_EMAIL) !== false){
- $emails[] = $part;
- }
- }
- }
- }
- if(count($emails) > 0){
- return $emails;
- }
- else{
- return null;
- }
- }
- $string = "Guys, please help me to extract valid sam-ple.1990@gmail.co.uk email addresses from some text content using php
- example , i have below text content in mysql database ' Life is more beautiful, and i like to explore lot please email me to sample@gmail.com. Learn new things every day. 'from the above text content i want to extract email address 'sample-x@gmail.com' using php regular expressions or other method.";
- $matches = extract_email_addresses( $string );
- print_r($matches);
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement