开发者

PHP - accesing and modyfing data "on the fly"?

Imagine you have some code generated by php:

<ul>
  <li><?php smth ?开发者_如何学编程></li>
  <li><?php smth ?></li>
  <li><?php smth ?></li>
</ul>
<ul>
  <li><?php smth ?></li>
  <li><?php smth ?></li>
  <li><?php smth ?></li>
</ul>
(...)

But the the code is dynamically generated and loaded in one function and there's no direct access to each nodes (<li> in this case). So on my site it looks a bit like an include:

<?php include("but not the file but code above generated by some engine"); ?>

All clear? So here's the deal.

I want to modify this code. So after it loads I want each <ul> element to be inside of a <div> with increasing ID. So the end code will look like:

<div id="1">
  <ul>
    <li><?php smth ?></li>
    <li><?php smth ?></li>
    <li><?php smth ?></li>
  </ul>
</div>

<div id="2">
  <ul>
    <li><?php smth ?></li>
    <li><?php smth ?></li>
    <li><?php smth ?></li>
  </ul>
</div>

(...)

Any ideas? I have my own concept based on stripping site's code after loading and adding <div>s with for loop but I believe there's more elegant way?


First, clean up your HTML. You need to close your ul tags with </ul>, not open new ones with <ul>. Second, you are not allowed to have id values beginning with a number, at least in HTML4.

In addition, I'm not sure exactly what you mean by

<?php include("but not the file but code above generated by some engine"); ?>

I think you mean you are including a file that outputs the HTML. This makes things a little tricky. You will need to turn output buffering on. You can then do some DOMDocument stuff with the captured content.

<?php

ob_start(); // turn output buffering on
include('your_file.php'); // include the contents of your_file -- contents go into the output buffer
$text = ob_get_clean(); // put the contents of the output buffer into $text

$dom = new DOMDocument(); // create a DOMDocument to work on

$f = $dom->createDocumentFragment();
$f->appendXML($text);
$dom->appendChild($f); // these three lines import $text into $dom

$i = 0;
while ($dom->childNodes->length > 0) { // loop through the elements from your file
    $child = $dom->childNodes->item(0); // working with the first remaining element
    $dom->removeChild($child); // remove them -- we'll reinsert relevant elements later 

    if ($child->nodeType === XML_ELEMENT_NODE) { // forget about non-element nodes (i.e. whitespace)
        $div[$i] = $dom->createElement('div'); // create a new parent element
        $div[$i]->appendChild($child); // stick the current ul into it
        $div[$i]->setAttribute('id', 'd' . (++$i)); // give it an id like d1, d2, etc.
    }
}

foreach ($div as $d) {
    $dom->appendChild($d); // reinsert each div element into the DOMDocument
}

echo $dom->saveHTML(); // echo the processed content

Of course, by far the easiest solution is to change the included file...


use javascript and jquery.

$(document).ready(function() {
    var i = 1;
    $("ul").each(function() {
        $(this).wrap('<div id="' + i + '" />');
        i++;
    });
});


You could used reqular expressions to break the code into chunks of <ul>, and then re-assemble the pieces together with div's around them.

Basically, you would be breaking the original string into an array, and then stepping through the array and remaking the string. Each time you step, just add an opening div with an incremented id to the beginning and and ending div to the end, then adding the new <ul> to a final string to return.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜