2012-09-17 77 views
3

拉我的頭髮在這裏...如何插入許多元素融入到DOM使用PHP

我需要插入一堆HTML到另一組在兩個不同的點設置HTML的,使用PHP。

$ a從另一個類的函數返回給我。我需要在$ a中插入$ b,使$ b元素成爲某些$ a元素的父元素。

<?php 
$a = ' 
<div id="one"> 
    <div id="two"> 
     <div id="three">Hi</div> 
    </div> 
</div> 
'; 

$b = ' 
<div id="red"> 
    <div id="blue"></div> 
</div> 
<div id="green"> 
    <div id="yellow">there</div> 
</div> 
'; 
?> 

需要落得:

<?php 
$c = ' 
<div id="one"> 
    <div id="two"> 
     <div id="red"> 
      <div id="blue"> 
       <div id="three">Hi</div> 
      </div> 
     </div> 
     <div id="green"> 
      <div id="yellow">there</div> 
     </div> 
    </div> 
</div> 
'; 
?> 

注$ B是如何插入一個$,使所有的$ B孩子 「二」。但同時,「三」成爲「藍」的孩子。 「綠色」和「黃色」保持原有關係,但現在嵌套在「兩個」之下。

現實世界的例子....

<?php 
$a = ' 
<div class="ginput_complex ginput_container"> 
    <span class="ginput_full"> 
      <input name="input_5" id="input_4_5" type="file" value="" class="medium" tabindex="5"> 
      <label for="input_4_5" class="ginput_post_image_file">File</label> 
    </span> 
    <span class="ginput_full ginput_post_image_title"> 
      <input type="text" name="input_5.1" id="input_4_5_1" value="" tabindex="6"> 
      <label for="input_4_5_1">Title</label> 
    </span> 
    <span class="ginput_full ginput_post_image_caption"> 
      <input type="text" name="input_5.4" id="input_4_5_4" value="" tabindex="7"> 
      <label for="input_4_5_4">Caption</label> 
    </span> 
    <span class="ginput_full ginput_post_image_description"> 
      <input type="text" name="input_5.7" id="input_4_5_7" value="" tabindex="8"> 
      <label for="input_4_5_7">Description</label> 
    </span> 
</div> 
'; 


$b = 
' 
<div class="container"> 
    <div class="row fileupload-buttonbar"> 
     <div class="span7"> 
      <span class="btn btn-success fileinput-button"> 
      <i class="icon-plus icon-white"></i> 

      {{{{{{{{ THIS IS WHERE I MY FILE INPUT SHOULD GO }}}}}}}}}}} 

      </span> 
      <button type="submit" class="btn btn-primary start"> 
       <i class="icon-upload icon-white"></i> 
       <span>Start upload</span> 
      </button> 
      <button type="reset" class="btn btn-warning cancel"> 
       <i class="icon-ban-circle icon-white"></i> 
       <span>Cancel upload</span> 
      </button> 
      <button type="button" class="btn btn-danger delete"> 
       <i class="icon-trash icon-white"></i> 
       <span>Delete</span> 
      </button> 
     </div> 
    </div> 
</div> 
'; 

$doc = new DOMDocument; 
$doc->loadHTML($a); 
$x = new DOMXPath($doc); 

?> 

我使用$ X->查詢( '// * [@ ID = 「input_4_5」]')來獲得的節點我想開始工作。從那裏開始,我一直在使用帶有appendChild,replaceChild和insertBefore變體的新DOMElement和importNode來遍歷DOM樹。但坦率地說,即使我成功地遍歷它,我也無法弄清楚如何插入/追加我正在處理的大塊代碼。

+1

,如果你是玩'考慮dom'使用'javascript' – Deepak

+1

我認爲JavaScript的,但我所有的變量都在服務器上,都設置,並且不需要用戶輸入。所以我真的很想在服務器端做到這一點。 – xd3v

+2

看看[DOMDocument](http://php.net/manual/en/class.domdocument.php) – Musa

回答

5

我使用的解析器SimpleHTMLDom

試試這個做的Javascript呼應出來在你的JS:

<?php 
require_once('simple_html_dom.php'); 

$a = ' 
<div id="one"> 
    <div id="two"> 
     <div id="three">Hi</div> 
    </div> 
</div> 
'; 

$b = ' 
<div id="red"> 
    <div id="blue"></div> 
</div> 
<div id="green"> 
    <div id="yellow">there</div> 
</div> 
'; 

$a = str_get_html($a); 
$c = str_get_html($b); 

$c->find('div[id=blue]', 0)->innertext = $a->find('div[id=two]', 0)->innertext; 
$a->find('div[id=two]', 0)->innertext = $c->save(); 

echo $a; 
?> 

希望這有助於。

+0

這個效果非常好。 – xd3v

-1

更換字符串會有幫助嗎?

$matched = array(); 
preg_match('/<div id="three">.*?<\/div>/', $a, $matched); 
$b = preg_replace('/<div id="blue"><\/div>/', '<div id="blue">'.$matched[0].'</div>', $b); 
$a = str_replace($matched[0], $b, $a); 
+0

我一直在試圖避免正則表達式的方法,但我即將陷入困境...... – xd3v

0

我認爲這將是更好地寫出了新的功能,以獲取有關ab數據,然後生成HTML。

+1

$ a來自另一個類的函數,我無法控制它。 – xd3v

0

如果我不得不這樣做,我會做一個佔位符,行/#/,然後做馬str_replace它。

但是,如果您確實需要使用DOM,請檢查DOMDocument。

如果你想

<script> 

var a = '<?php echo $a ?>'; 
var b = '<?php echo $b ?>': 

</script> 
2

查看下面的代碼,我已經使用php & jQuery生成輸出。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<title>Untitled Document</title> 
<script language="javascript" src="http://code.jquery.com/jquery-1.7.js"></script> 
</head> 

<body> 
<div id="varA" style="display:none"> 
<? 
    echo $a= '<div id="one"> 
    <div id="two"> 
     <div id="three">Hi</div> 
    </div> 
</div>'; 
?> 
</div> 
<div id="varB" style="display:none"> 
<? 
    echo $b = ' 
<div id="red"> 
    <div id="blue"></div> 
</div> 
<div id="green"> 
    <div id="yellow">there</div> 
</div> 
'; 
?> 
</div> 

<span id="output"></span> 

<script language="javascript"> 
alert('varA id have:'+$('#varA').html()); 
var middle = $('#two').html(); 
$('#output').html($('#varA').html()); 
$('#varA').remove(); 
alert('varB id have:'+$('#varB').html()); 
$('#two').html($('#varB').html()); 
$('#varB').remove(); 
$('#blue').html(middle); 
alert('output id have:'+$('#output').html()); 
</script> 

</body> 
</html>