this question different from following questions How do you parse and process HTML/XML in PHP?
this question is for getting Javascript code not html or php code
i want
user pest html code in textarea and then php script print the Javascript that can be used for generating same html code using Javascript
i have done the getting tag name, create tag code, 'adding class, attributes' but i unable write "appendChild" like code because i can't find which is parent element & which if child element of which element
i have write following code
<html>
<style>
.ac{
color:red;
}
</style>
<body>
<form action="" method="post">
<textarea name="data" id="code" cols="200" rows="20">
<?php
if(isset($_POST['data']))
{
echo $_POST['data'];
}
?>
</textarea>
<input type="submit">
</form>
</body>
</html>
<?php
if(isset($_POST['data']))
{
echo "<div id=tag0>";
echo $_POST['data'] ;
echo "</div>";
}
?>
<?php
if(isset($_POST['data']))
{
//divide start tag and end tag
$array=str_split($_POST['data']);
$tag=array();
$i=0;
$tag_close=false;
$tag_star=false;
foreach($array as $key => $value)
{
// echo "<hr>" .$i." is value of i<hr>";
// echo $tag_close . $tag_star;
//<html></html>
// if($value=' ')
// {
// }
// else
if($value == '>')
{
$tag_close=true;
$tag_star=false;
$i++;
}
else if($value == '<')
{
$tag_star=true;
}
else if($tag_star)
{
$tag_close=false;
if(!isset($tag[$i]))
{
$tag[$i]='';
}
$tag[$i]=$tag[$i].$value;
}
}
//writing javascript
$k=1;
$tag_name='';
$end_tag_started=false;
$attribut=array();
$attribut_val=array();
$j=0;
$o=0;
$attribut_value_started=false;
// one y one tag adding and it's attriut
foreach($tag as $key => $value)
{
$t='tag'.$k;
$p='tag'.$o;
$value_aaray=str_split($value);
$tag_name='';
$attribut=array();
$attribut_val=array();
$j=0;
foreach($value_aaray as $val)
{
if($val==' ')
{
$attribut[$j]='';
$attribut_val[$j]='';
foreach($value_aaray as $val_array)
{
if($val_array == " ")
{
$j++;
$attribut_value_started=false;
}
else if($val_array == "=")
{
$attribut_value_started=true;
}
else
{
if($attribut_value_started)
{
@$attribut_val[$j]=$attribut_val[$j].$val_array;
}
else
{
@$attribut[$j]=$attribut[$j].$val_array;
}
}
}
break;
}
if($val == '/')
{
$end_tag_started=true;
break;
}
$tag_name=$tag_name.$val;
}
if(! $end_tag_started)
{
echo "<br> \t";
echo $t ."= document.createElement(\"".$tag_name."\");";
echo "<br> \t";
for($l=1;$l<count($attribut);$l++)
{
echo " $t.setAttribute(\"".@$attribut[$l]."\",\"".@$attribut_val[$l]."\");<br>";
}
}
$end_tag_started=false;
$k++;
}
echo "<hr>'tag'";
print_r($tag);
echo "<hr> ' letter array ' ";
print_r($array);
// for appendChild code but not working
$a=array();
for($i=0;$i<(count($tag));$i++)
{
$val=str_split($tag[$i]);
if($val[0]=="/")
{
echo "<hr>";
echo $a[]=" tag". ( $i-2 ) .".appendChild(tag". ($i-1).");";
}
}
}
?>
i get following output
textarea is start
<div>
<div>
</div>
<div>
<div>
</div>
</div>
</div>
textarea is end
tag1= document.createElement("div");
tag2= document.createElement("div");
tag4= document.createElement("div");
tag5= document.createElement("div");
'tag'Array ( [0] => div [1] => div [2] => /div [3] => div [4] => div [5] => /div [6] => /div [7] => /div )
' letter array ' Array ( [0] => < [1] => d [2] => i [3] => v [4] => > [5] => [6] => [7] => [8] => [9] => < [10] => d [11] => i [12] => v [13] => > [14] => [15] => [16] => < [17] => / [18] => d [19] => i [20] => v [21] => > [22] => [23] => [24] => [25] => [26] => < [27] => d [28] => i [29] => v [30] => > [31] => [32] => [33] => [34] => [35] => < [36] => d [37] => i [38] => v [39] => > [40] => [41] => [42] => < [43] => / [44] => d [45] => i [46] => v [47] => > [48] => [49] => [50] => [51] => [52] => < [53] => / [54] => d [55] => i [56] => v [57] => > [58] => [59] => [60] => [61] => [62] => < [63] => / [64] => d [65] => i [66] => v [67] => > )
tag0.appendChild(tag1);
tag3.appendChild(tag4);
tag4.appendChild(tag5);
tag5.appendChild(tag6);