本文介绍了通过DOMDocument将Div插入到从远程站点获取的内容中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对不起,如果标题令人困惑,请参见以下说明:

I am sorry if the title is confusing here is the explanation:

说我们有一个像remotesite.com/page1.html这样的远程页面,我们使用函数file_get_contents来获取其源代码,然后我们使用DOMDocument来编辑该源代码,然后再将其打印到页面上

say we have remote page like remotesite.com/page1.html and we use the function file_get_contents to get its source, then we use DOMDocument to edit this source before printing it to our page

$url = "remotesite.com/page1.html";
$html = file_get_contents($url);
$doc = new DOMDocument(); // create DOMDocument
libxml_use_internal_errors(true);
$doc->loadHTML($html); // load HTML you can add $html
//here we do some edits to remove or add contents

我想在打印内容之前将下面的Div添加到内容中:

I want to add the Div below to the content before printing it:

<div style="float: right; padding-right: 2px;"><a class="open_event_tab" target="_blank" href="some-hard-coded-text-here_'+content+'_title_'+lshtitle+'_event_'+id+'.html" >open event</a></div> 

这是我尝试过的方法,但是href的软编码部分('+content+'_title_'+lshtitle+'_event_'+id+')无法正常工作

Here is what i have tried but the soft coded part ( '+content+'_title_'+lshtitle+'_event_'+id+') of the href is not working

我知道下面的我的代码可能看起来很愚蠢,但是对不起,我对php的了解不够

i know that the my code below may look stupid but sorry i dont have enough knowledge in php

function createDivNode($doc) {
$divNode = $doc->createElement('div');
$divNode->setAttribute('style', 'float: right; padding-right: 2px;');
$aNode = $doc->createElement('a', 'openEvent');
$aNode->setAttribute('class', 'open_event_tab');
$aNode->setAttribute('target', '_blank');
$aNode->setAttribute('href', 'some-hard-coded-text-here_'+content+'_title_'+lshtitle+'_event_'+id+'.html');
$divNode->appendChild($aNode);
return $divNode;

}

,我想遍历从远程站点获取的源代码,以获取每个如下所示的td,并在关闭它之前添加div

and i want to loop through the source got from remote site to get every td that look like the one below and add the div just before closing it

  <td colspan="2">
     <b>Video </b> 
     <span class="section">Sports</span><b>: </b> 
     <span id="category466" class="category">Motor Sports</span>

    //here i want to add my div
</td>

6个小时的研究,由于我处于学习阶段,所以我无法弄清楚,所以我决定在这个有用的社区中向某人询问

6 hours of research and i can't figure this out as i am in learning phase, so i decided to ask someone here at this helpful community

推荐答案

现在仍然对您的问题感到困惑,但我认为,您想在每个td内添加/附加动态div,如果是这样的话那么您可以尝试一下(至少您会有所了解,而且非常干净)

Still now confused about your question but I think that, you want to add/append a dynamic div inside every td and if this is the case then you may try this (at least, you'll get an idea and it's very clean)

var content = 'someContent', lshtitle = 'someTitle', id = 'anId';
var attr = {
    'class' : 'open_event_tab',
    'target' : '_blank',
    'href' : 'some-hard-coded-text-here_' + content + '_title_' + lshtitle + '_event_' + id + '.html',
    'text' : 'open event'
};
var link = $('<a/>', attr);
var div = $('<div/>', { 'style' : 'float:right;padding-right:2px;' }).append(link);

$('#myTable td').append(div);

演示.

更新:(问题令人困惑,因此下面给出了更新后的答案)

Update : (Question was confusing, so updated answer given below)

只需下载简单HTML DOM Perser 和(此处的文档)

    include('simple_html_dom.php');
    $html = file_get_html('remotesite.com/page1.html');
    foreach($html->find('table td') as $td) {
        $td->innertext = $td->innertext . '<div>New Div</div>';
    }

此外,仅修改具有class=category

    foreach($html->find('table td.category') as $td) {
        $td->innertext = $td->innertext . '<div>New Div</div>';
    }

您已完成.注意<div>New Div</div>,这只是一个例子,希望您可以根据需要进行制作.

And you are done. Notice <div>New Div</div>, it's just an example, hope, you can make it according to your need.

该示例的可能结果:

<table>
    <tbody>
        <tr>
            <td colspan="2">
                <b>Video </b> <span class="section">Sports</span><b>: </b> <span id="category466" class="category">Motor Sports</span>
                <div>New Div</div>
           </td>
        </tr>
        <tr>
            <td colspan="2">
                <b>Video </b> <span class="section">Sports</span><b>: </b> <span id="category466" class="category">Motor Sports</span>
                <div>New Div</div>
            </td>
        </tr>
    </tbody>
</table>

这篇关于通过DOMDocument将Div插入到从远程站点获取的内容中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-03 10:38