Advertisement
Guest User

Untitled

a guest
Sep 12th, 2017
945
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.88 KB | None | 0 0
  1. $string = file_get_contents("example.txt"); // Load text file contents
  2. $matches = array(); //create array
  3. $pattern = '/[A-Za-z0-9_-]+@[A-Za-z0-9_-]+.([A-Za-z0-9_-][A-Za-z0-9_]+)/'; //regex for pattern of e-mail address
  4. preg_match($pattern, $string, $matches); //find matching pattern
  5.  
  6. $string = file_get_contents("example.txt"); // Load text file contents
  7.  
  8. // don't need to preassign $matches, it's created dynamically
  9.  
  10. // this regex handles more email address formats like a+b@google.com.sg, and the i makes it case insensitive
  11. $pattern = '/[a-z0-9_-+]+@[a-z0-9-]+.([a-z]{2,3})(?:.[a-z]{2})?/i';
  12.  
  13. // preg_match_all returns an associative array
  14. preg_match_all($pattern, $string, $matches);
  15.  
  16. // the data you want is in $matches[0], dump it with var_export() to see it
  17. var_export($matches[0]);
  18.  
  19. array (
  20. 0 => 'test1+2@gmail.com',
  21. 1 => 'test-2@yahoo.co.jp',
  22. 2 => 'test@test.com',
  23. 3 => 'test@test.co.uk',
  24. 4 => 'test@google.com.sg',
  25. )
  26.  
  27. /[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+.[A-Za-z]{2,4}/
  28.  
  29. $sourceeee= 'Here are examplr mymail@yahoo.com and my-e.mail@goog.com or something more';
  30.  
  31. preg_match_all('/[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+.[A-Za-z]{2,4}/i', $sourceeee, $found_mails);
  32.  
  33. <?
  34. $url="http://example.com/";
  35. $text=file_get_contents($url);
  36. $res = preg_match_all(
  37. "/[a-z0-9]+[_a-z0-9.-]*[a-z0-9]+@[a-z0-9-]+(.[a-z0-9-]+)*(.[a-z]{2,4})/i",
  38. $text,
  39. $matches
  40. );
  41. if ($res) {
  42. foreach(array_unique($matches[0]) as $email) {
  43. echo $email . "<br />";
  44. }
  45. }
  46. else {
  47. echo "No emails found.";
  48. }
  49. ?>
  50.  
  51. <?php
  52. $content = "Hi my name is Joe, I can be contacted at joe@mysite.com.";
  53. preg_match("/[_a-z0-9-]+(.[_a-z0-9-]+)*@[a-z0-9-]+(.[a-z0-9-]+)*(.[a-z]{2,3})/i", $content, $matches);
  54. print $matches[0];
  55. ?>
  56.  
  57. <?php
  58. function extract_email_addresses($str){
  59. $emails = array();
  60. $str = strip_tags( $str );
  61. $str = preg_replace('/s+/', ' ', $str);
  62. $str = preg_replace("/[nr]/", "", $str);
  63. $remove_chars = array (',', "<", ">", ";", "'", ". ");
  64. $str = str_replace( $remove_chars, ' ', $str );
  65. $parts = explode(' ', $str);
  66. if(count($parts) > 0){
  67. foreach($parts as $part){
  68. $part = trim($part);
  69. if( $part != '' ) {
  70. if( filter_var($part, FILTER_VALIDATE_EMAIL) !== false){
  71. $emails[] = $part;
  72. }
  73. }
  74. }
  75. }
  76. if(count($emails) > 0){
  77. return $emails;
  78. }
  79. else{
  80. return null;
  81. }
  82. }
  83.  
  84. $string = "Guys, please help me to extract valid sam-ple.1990@gmail.co.uk email addresses from some text content using php
  85. 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.";
  86.  
  87. $matches = extract_email_addresses( $string );
  88. print_r($matches);
  89.  
  90. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement