1

从以下 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>可以有变量 class 和 id ,所以很难使用 jquery 等来拉它们。我被它挡住了。任何帮助将不胜感激

4

3 回答 3

2

使用 jQuery-->

$(document).ready(function() {
    $('tr').find('td:last').each(function(){
        var t = $(this).text();
if(typeof(t) === 'string' && t.indexOf('@') >-1) alert(t);
    })
});​
于 2012-05-22T06:29:44.483 回答
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]);
于 2012-05-22T06:09:37.910 回答
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());
    })
});
于 2012-05-22T06:37:42.873 回答