Question

I am using PHP Simple HTML DOM Parser for my project..

WHat I'm stuck on currently is how I can target/find all TEXT nodes... and either add an element or even TEXT at the END of the TEXT..

Here is what Im trying so far.. but figured there might be a better (working ) way? :)

$var = "INSERT TRANSLATION HERE >>";
foreach($html->find('text') as $tn){
    //$tn->outertext = $tn->makeup() . $tn->innertext . $var';
    $tn->innertext = $tn->innertext . $var;
}
Was it helpful?

Solution

Your code is good, you just forgot to call save:

$string = "<div id='container'><div>im a text node</div><div>im another</div></div>";
$html = str_get_html($string);
foreach($html->find("text") as $ht) {
   $ht->innertext .= "<span>end of each text node</span>";
}
$html->save();
echo $html;   // echo modified html

Just replace the <span> element with your $var.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top