4

Lets say i have an external html page and I want to append text inside of a specific tag from my php admin page such as putting miscellaneous text inside of this span tag.

For example:

<html>
<body>
<span class="text"></span>
</body>
</html>

How would I do that with PHP? I'm trying to make an admin page for this website, and I need to append text inside of certain tags. I don't even know where to start with this, please point me in the right direction.

3
  • Like file_get_content, or include? Commented Jun 28, 2012 at 14:20
  • @itsme I think what he means is, he wants to open a file, write/append to it within a specific tag. Commented Jun 28, 2012 at 14:21
  • possible duplicate of How to append source html to a DOMElement in PHP? Commented Mar 19, 2015 at 22:16

2 Answers 2

4

You can do this using PHP's DOMDocument, like so:

// Load the HTML document
$doc = new DOMDocument;
$doc->loadHtmlFile( 'htmlpage.html');

// Get the parent node where you want the insertion to occur
$parent = $doc->getElementsByTagName('body')->item( 0);

// Create the child element 
$child = $doc->createElement( 'span');
$child->setAttribute( 'class', 'text');

// Append (insert) the child to the parent node
$parent->appendChild( $child);

// Save the resulting HTML
echo $doc->saveHTML();

So, given this HTML:

<html>
<body>
</body>
</html>

The resulting HTML would be:

<html>
<body>
<span class="text"></span>
</body>
</html>

(Neglecting the DOCTYPE declaration that DOMDocument adds if it is not present)

Sign up to request clarification or add additional context in comments.

4 Comments

awesome, thanks a lot man... if i was getting paid a decent amount for this site i would give you something... very helpful thanks
hmm, my only problem with this is switching from getElementsByTagName to getElementById... it gives me php errors
thanks a lot. still have problems with elementbyid but i can use tagname for this application.
@JohnJohnson According to a commenter in the PHP Docs, if your HTML page doesnt' specify a doctype then getElementById will always return null. (php.net/manual/en/domdocument.getelementbyid.php)
1

Depending on what exactly you need this for, you may be able to do this another way -- make that target .HTML file into a .PHP file instead, with it filling in the appropriate content itself using, let's say, a function called get_span_contents(), like so:

<html>
<body>
  <span class="text">
    <?PHP get_span_contents(); ?>
  </span>
</body>
</html>

One downside is that this will be generated for every request of the document (potentially, depending on caching schemes), whereas if you separated it into two steps (1. Write out the HTML, 2. Serve the HTML separately) you would only ever be doing the generation once. Depending on how dynamic the content is, or how minor the computation cost, that might not be a problem.

1 Comment

thanks, this is a good option if i cannot figure out the DOMDocument

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.