PHP-访问和修改数据”;在飞行中;?
假设您有一些由PHP-访问和修改数据”;在飞行中;?,php,Php,假设您有一些由php生成的代码: <ul> <li><?php smth ?></li> <li><?php smth ?></li> <li><?php smth ?></li> </ul> <ul> <li><?php smth ?></li> <li><?php smth ?
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>
(...)
(...)
但是代码是在一个函数中动态生成和加载的,并且没有对每个节点的直接访问()。因此,在我的网站上,它看起来有点像一个包含:
<?php include("but not the file but code above generated by some engine"); ?>
安全了吗?这就是交易
我想修改这个代码。因此,在加载后,我希望每个
元素都位于ID增加的
内部。因此,结束代码如下所示:
<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>
(...)
(...)
有什么想法吗?我有自己的概念,在加载并添加带有for循环的s
后剥离站点代码,但我相信还有更优雅的方法?使用javascript和jquery
$(document).ready(function() {
var i = 1;
$("ul").each(function() {
$(this).wrap('<div id="' + i + '" />');
i++;
});
});
$(文档).ready(函数(){
var i=1;
$(“ul”)。每个(函数(){
$(此).wrap(“”);
i++;
});
});
您可以使用reqular表达式将代码分解为
块,然后将这些块与周围的div重新组装在一起
基本上,您可以将原始字符串拆分为一个数组,然后遍历该数组并重新生成该字符串。每次执行步骤时,只需将id递增的开头div添加到开头,将结尾div添加到结尾,然后将新的
添加到要返回的最后一个字符串。首先,清理HTML。您需要用
关闭您的ul
标签,而不是用
打开新标签。其次,至少在HTML4中,不允许使用以数字开头的id
值
另外,我不太清楚你说的确切意思
<?php include("but not the file but code above generated by some engine"); ?>
当然,到目前为止,最简单的解决方案是更改包含的文件…这将使JS被禁用的人失去整个布局。我想这意味着。。。页面离开服务器端并在浏览器中呈现后。当然,这意味着你必须使用javascript。我一直在假设一个ob_xmlfilter,但我被称为名字。但通常可以捕获输出,将其导入QueryPath或phpQuery,然后在PHP中重新构造(类似于jQuery)。缺点是这将是一个可测量的减速,因此您应该有一个非常好的用例。
<?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