如何从PHP中提取电子邮件

时间:2012-05-22 06:02:56

标签: php javascript jquery html

从以下HTML中我只需要提取电子邮件地址以将其保存在数据库中。我需要将它拉入数组,我计划使用jquery / ajax使用dom提取所有电子邮件并使用ajax将其保存到另一个页面,但问题是 <td>与其他<td>没有唯一标识,即如果td具有类或id名称,我可以这样做

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Untitled Document</title>
    </head>

    <body>
    <table width="100%" border="1">
    <tr></tr>
    <tr>
      <td>NAME</td>
      <td>ADDRESS</td>
      <td>PHONE</td>
      <td>EMAIL</td>
    </tr>
    <tr>
      <td>wwqw</td>
      <td>qww</td>
      <td>ew</td>
      <td>email@exmaple.com</td>
    </tr>
    <tr>
      <td>e</td>
      <td>wew</td>
      <td>ew</td>
      <td>emai1l@exmaple.com</td>
    </tr>
    <tr>
      <td>e</td>
      <td>ewe</td>
      <td>we</td>
      <td>email2@exmaple.com</td>
    </tr>
    <tr>
      <td>we</td>
      <td>we</td>
      <td>we</td>
      <td>emai3l@exmaple.com</td>
    </tr>
    <tr>
      <td>ww</td>
      <td>w</td>
      <td>w</td>
      <td>emai4l@exmaple.com</td>
    </tr>
    </table>
    </body>
    </html>

我需要抓住电子邮件地址并存储在数组中,如下所示

Array
(
    [0] => email@exmaple.com
    [1] => emai1l@exmaple.com
    [2] => email2@exmaple.com
    [3] => emai3l@exmaple.com
    [4] => emai4l@exmaple.com
)

<td>可以有变量类和id,因此很难使用jquery等来提取它们。我受阻了。任何帮助将不胜感激

3 个答案:

答案 0 :(得分:2)

使用jQuery - &gt;

$(document).ready(function() {
    $('tr').find('td:last').each(function(){
        var t = $(this).text();
if(typeof(t) === 'string' && t.indexOf('@') >-1) alert(t);
    })
});​

答案 1 :(得分:1)

不要使用jquery,正则表达式会让你的工作变得轻松。

$string='<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<table width="100%" border="1">
<tr></tr>
<tr>
  <td>NAME</td>
  <td>ADDRESS</td>
  <td>PHONE</td>
  <td>EMAIL</td>
</tr>
<tr>
  <td>wwqw</td>
  <td>qww</td>
  <td>ew</td>
  <td>email@exmaple.com</td>
</tr>
<tr>
  <td>e</td>
  <td>wew</td>
  <td>ew</td>
  <td>emai1l@exmaple.com</td>
</tr>
<tr>
  <td>e</td>
  <td>ewe</td>
  <td>we</td>
  <td>email2@exmaple.com</td>
</tr>
<tr>
  <td>we</td>
  <td>we</td>
  <td>we</td>
  <td>emai3l@exmaple.com</td>
</tr>
<tr>
  <td>ww</td>
  <td>w</td>
  <td>w</td>
  <td>emai4l@exmaple.com</td>
</tr>
</table>
</body>
</html>';
echo "<pre>";
$pattern="/([\s]*)([_a-zA-Z0-9-]+(\.[_a-zA-Z0-9-]+)*([ ]+|)@([ ]+|)([a-zA-Z0-9-]+\.)+([a-zA-Z]{2,}))([\s]*)/i"; 
preg_match_all($pattern, $string, $matches);
print_r($matches[0]);

答案 2 :(得分:1)

<?php 
    include('simple_html_dom.php');
    $html = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Untitled Document</title>
    </head>

    <body>
    <table width="100%" border="1">
    <tr></tr>
    <tr>
      <td>NAME</td>
      <td>ADDRESS</td>
      <td>PHONE</td>
      <td>EMAIL</td>
    </tr>
    <tr>
      <td>wwqw</td>
      <td>qww</td>
      <td>ew</td>
      <td>email@exmaple.com</td>
    </tr>
    <tr>
      <td>e</td>
      <td>wew</td>
      <td>ew</td>
      <td>emai1l@exmaple.com</td>
    </tr>
    <tr>
      <td>e</td>
      <td>ewe</td>
      <td>we</td>
      <td>email2@exmaple.com</td>
    </tr>
    <tr>
      <td>we</td>
      <td>we</td>
      <td>we</td>
      <td>emai3l@exmaple.com</td>
    </tr>
    <tr>
      <td>ww</td>
      <td>w</td>
      <td>w</td>
      <td>emai4l@exmaple.com</td>
    </tr>
    </table>
    </body>
    </html>';

    $dom = new simple_html_dom();
    $dom->load($html);
    $table = $dom->find('table', 0);
    if($table){
        foreach($table->find('tr') as $row) {
            $rowData = array();
            foreach($row->find('td') as $cell) {
                $rowData[] = $cell->innertext;
            }
            $theData[] = $rowData;
        }
        print_r($theData);
    }
?>

或者你可以用jquery

来做
$(document).ready(function() {
    $('tr').find('td:last').each(function(){
        console.log($(this).text());
    })
});