1

I need to locate the (in originalFile.html (//*[@id='divLinkHeader']/ul)) and replace with the new (of otherFile.html) for the new file (originalFile_WithNewUL.html) is saved with the new structure.

<!-- originalFile.html-->
<div class="col-md-4 col-sm-6">
    <div id="headerLog" class="call-5">
        <a href="#">
        <img class="wa333372" src="#">
        </a>
    </div>
    <div id="what5" class="pl-5 subtitle"> 
    </div>
    <div id="divLinkHeader" class="sub">
        <ul class="list-inline list-unstyled"> <!-- <========= DELETE <ul> ?? -->
            <li><a href="#">Line 1</a></li>
            <li><a href="#">Line 2</a></li>
            <li><a href="#">Line 3</a></li>
            <li><a href="#">Line 4</a></li>
        </ul>
    </div>
</div>

And this is other file

<!-- otherFile.html-->
<ul class="list-inline list-unstyled">
  <li><a href="#">New Line 1</a></li>
  <li><a href="#">New Line 2</a></li>
  <li><a href="#">New Line 3</a></li>
</ul>

I just want to replace a node on the other to save a file called "originalFile_WithNewUL.html"

And this is my code:

// ---
// Create New originalFile_WithNewUL.html
// ---
$newFile = "originalFile_WithNewUL".".html";
$html = file_get_contents("originalFile".".html");
$dom = new DOMDocument();
@$dom->loadHTML($html);
$xpath = new DOMXPath($dom);

////
////// create the new node from otherFile.html ???
//// $newNode = $dom->createElement('otherFile.html'); ?????
////
////// fetch and replace the old element ?????
//// $oldNode = $xpath->evaluate("//*[@id='divLinkHeader']/ul");
////
//// $results = $xpath->evaluate("//*");
////


$htmlVar = "<!-- WithNewUL -->\n";
$htmlVar = $htmlVar . $dom->saveHTML($results->item(0)) . "\n";
$htmlVar = $htmlVar . "<!-- END WithNewUL -->";


$fid=fopen($newFile,"w+"); 
fwrite($fid, $htmlVar);  
fclose($fid);

I need to create this structure

<div class="col-md-4 col-sm-6">
    <div id="headerLog" class="call-5">
        <a href="#">
        <img class="wa333372" src="#">
        </a>
    </div>
    <div id="what5" class="pl-5 subtitle"> 
    </div>
    <div id="divLinkHeader" class="sub">
        <ul class="list-inline list-unstyled">
            <li><a href="#">New Line 1</a></li>
            <li><a href="#">New Line 2</a></li>
            <li><a href="#">New Line 3</a></li>
        </ul>
    </div>
</div>
mikel
  • 15
  • 8
  • I'm unsure of what you're asking. Can you clarify your question? – Goose Jul 01 '15 at 12:44
  • I saw that code in your question. I'm still unsure of what you're asking. Perhaps it will help to be less abstract. What purpose is this software going to serve? – Goose Jul 01 '15 at 12:57
  • I'm working on a task of scraping on a URL. With XPath I could create separate items (2 files html) but now I need to combine them. – mikel Jul 01 '15 at 13:43
  • I found this solution but it's not good for me because I have 2 files html [link](http://stackoverflow.com/questions/4614434/how-can-i-use-xpath-and-dom-to-replace-a-node-element-in-php) – mikel Jul 01 '15 at 13:45

1 Answers1

0

It is not the most elegant, but it works

$str1 = file_get_contents("originalFile".".html");
$str2 = file_get_contents("otherFile".".html");
$str3 = "abc"; // here Node old

$htmlVar = str_replace($str3, $str2, $str1);
mikel
  • 15
  • 8