1

I've seen many question which are almost what I am looking to do and have led me to almost be able to get it but not quite.

I want to format text as follows within a <p> tag which is within a div.

Page 1 of 2

So in ordinary HTML I used the b tag within the paragraph but can't seem to figure out how to do that with DomDocument. When I try to create an element like so

$pTag = $dom->createElement("p", "Page <b>1</b> of 2");

It outputs just that without recognising the as HTML. So I thought about it and came up with

$pTag->nodeValue .= 

as a way to append a new element but that did no good. It didn't give me any errors but it didn't append the <b> tag either. This seems like something that should be simple but doesn't seem to be.

When I tried echo it outputted the text to the top of the screen, not where I wanted it.

I'd appreciate any advice.

MillyMonster
  • 546
  • 4
  • 21
  • You need to provide more details for what exactly you are trying to do. If you just want to `output text`, you can just `echo '

    Page 1 of 2

    ';`. Tell us more.
    – Ranty Dec 05 '12 at 16:37
  • Duplicate of this http://stackoverflow.com/questions/4400980/how-to-insert-html-to-php-domnode – Derek Organ Dec 05 '12 at 16:38
  • I edited to try to clarify what I am doing. @DerekOrgan I hadn't seen that question even though I've been searching for hours. Apologies if this is a duplicate but tbh I don't think it is. – MillyMonster Dec 05 '12 at 16:44

1 Answers1

2

Something like the following should work:

$bTag = $dom->createElement("b", "1");
$pTag = $dom->createElement("p");
$pTag->appendChild($dom->createTextNode("Page "));
$pTag->appendChild($bTag);
$pTag->appendChild($dom->createTextNode(" of 2"));
Alex
  • 7,320
  • 1
  • 18
  • 31