Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/229.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/matlab/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHPDOM-替换THAD到TH标记中的TD标记_Php_Html_Domdocument - Fatal编程技术网

PHPDOM-替换THAD到TH标记中的TD标记

PHPDOM-替换THAD到TH标记中的TD标记,php,html,domdocument,Php,Html,Domdocument,我正在尝试替换THAD到TH标记中的所有TD标记 我认为使用PHPDOM扩展最好。我对这方面还不太熟悉,所以我为自己缺乏知识而道歉 我做了一些搜索,找到了如何替换标记名。但是,我不知道如何只替换父级中的标记名(在本例中是THEAD标记)。我想让TD保持原样在车身内 下面是我的代码,用于缩小到THAD中的TD。那就是我迷路的地方 如何将AD中的标记名更改为TH $html = '<table> <thead> <tr>

我正在尝试替换THAD到TH标记中的所有TD标记

我认为使用PHPDOM扩展最好。我对这方面还不太熟悉,所以我为自己缺乏知识而道歉

我做了一些搜索,找到了如何替换标记名。但是,我不知道如何只替换父级中的标记名(在本例中是THEAD标记)。我想让TD保持原样在车身内

下面是我的代码,用于缩小到THAD中的TD。那就是我迷路的地方

如何将AD中的标记名更改为TH

$html = '<table>
    <thead>
        <tr>
            <td>Column 1</td>
            <td>Column 2</td>
            <td>Column 3</td>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>Column 1</td>
            <td>Column 2</td>
            <td>Column 3</td>
        </tr>
        <tr>
            <td>Column 1</td>
            <td>Column 2</td>
            <td>Column 3</td>
        </tr>
        <tr>
            <td>Column 1</td>
            <td>Column 2</td>
            <td>Column 3</td>
        </tr>
    </tbody>
</table>';

// create empty document 
$document = new DOMDocument();

// load html
$document->loadHTML(html);

// Get theads
$theads = $document->getElementsByTagName('thead');

// Loop through theads (incase there are more than one!)
for($i=0;$i<$theads->length;$i++) {
    $thead = $theads->item($i);

    // Loop through TR
    foreach ($thead->childNodes AS $tr) {
      if ($tr->nodeName == 'tr') {

          // Loop through TD
          foreach ($tr->childNodes AS $td) {
              if ($td->nodeName == 'td') {

                // Replace this tag

                }
            }

      }
    }

}
$html='1!'
第1栏
第2栏
第3栏
第1栏
第2栏
第3栏
第1栏
第2栏
第3栏
第1栏
第2栏
第3栏
';
//创建空文档
$document=新的DOMDocument();
//加载html
$document->loadHTML(html);
//买电影
$thead=$document->getElementsByTagName('thead');
//循环通过THADS(如果有多个!)
对于($i=0;$i长度;$i++){
$thead=$thead->item($i);
//循环通过TR
foreach($thead->childNodes作为$tr){
如果($tr->nodeName=='tr'){
//环路通过TD
foreach($tr->childNodes作为$td){
如果($td->nodeName=='td'){
//替换此标签
}
}
}
}
}

如果您已经查看了手册,那么有一种方法可以用来将
td
替换为
th
标签:

$html = '<table>
    <thead>
        <tr>
            <td>Column 1</td>
            <td>Column 2</td>
            <td>Column 3</td>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>Column 1</td>
            <td>Column 2</td>
            <td>Column 3</td>
        </tr>
        <tr>
            <td>Column 1</td>
            <td>Column 2</td>
            <td>Column 3</td>
        </tr>
        <tr>
            <td>Column 1</td>
            <td>Column 2</td>
            <td>Column 3</td>
        </tr>
    </tbody>
</table>';

// create empty document
$document = new DOMDocument();
// load html
$document->loadHTML($html);
// Get theads
$theads = $document->getElementsByTagName('thead')->item(0); // get thead tag
foreach($theads->childNodes as $tr) { // loop thead rows `tr`
    $tds = $tr->getElementsByTagName('td'); // get tds inside trs
    $i = $tds->length - 1;
    while($i > -1) {
        $td = $tds->item($i); // td
        $text = $td->nodeValue; // text node
        $th = $document->createElement('th', $text); // th element with td node value
        $td->parentNode->replaceChild($th, $td); // replace
        $i--;
    }

}

echo $document->saveHTML();
$html='1!'
第1栏
第2栏
第3栏
第1栏
第2栏
第3栏
第1栏
第2栏
第3栏
第1栏
第2栏
第3栏
';
//创建空文档
$document=新的DOMDocument();
//加载html
$document->loadHTML($html);
//买电影
$theads=$document->getElementsByTagName('thead')->项(0);//获取AD标签
foreach($thead->childNodes作为$tr){//loop thead rows`tr`
$tds=$tr->getElementsByTagName('td');//在trs中获取tds
$i=$tds->长度-1;
而($i>-1){
$td=$tds->item($i);//td
$text=$td->nodeValue;//文本节点
$th=$document->createElement($th',$text);//具有td节点值的第th个元素
$td->parentNode->replaceChild($th,$td);//替换
$i--;
}
}
echo$document->saveHTML();