Home > Net >  How do i access to the HTML element inside PHP?
How do i access to the HTML element inside PHP?

Time:04-25

For example i have an element <p> with the attribute id="targetParagraph" inside the <body> of an HTML document, how can i access that <p> element from PHP code ?

CodePudding user response:

Php DOM parser

Example 1

// Get the html 
$dom = new DOMDocument();
$dom->load("index.html");
$div = $dom->getElementById('totaal');
print_r($div); //prints everything it gets

Example 2

<?php
$htmlEle = "<p id='paraID'>Paragraph Sports</span>";
$domdoc = new DOMDocument();
$domdoc->loadHTML($htmlEle);
 
echo $domdoc->getElementById('paraID')->nodeValue;
  1. Using the PHP DOMDocument Class, call the DOMDocument object.
  2. Call the predefined loadHTML() function with variable parameters.
  3. Using the getElementById() DOM function we get the HTML element value.

Using strpos and substr

Assuming the content to extract the paragraph from is in the variable $html (which may have come from a file, database, template or downloaded from an external website), use the following code to work out the position of the first

tag, the first

tag after that tag, and then get all the HTML between them including the opening and closing tags:

$start = strpos($html, '<p>');
$end = strpos($html, '</p>', $start);
$paragraph = substr($html, $start, $end-$start 4);

Line 1 gets the position of the first opening <p> tag.

Line 2 gets the position of the first </p> after the first opening <p>

Line 3 then uses substr to get the HTML. The third parameter is the number of characters to copy and is calculated by subtracting $start from $end and adding on the length of </p> so it is included in the extracted HTML.


Converting to plain text

If the extracted paragraph needs to be in plain text rather than HTML, use the following to remove the HTML tags and convert HTML entities into normal plain text:

$paragraph = html_entity_decode(strip_tags($paragraph));

For more explanation check The DOMDocument Class

CodePudding user response:

Well PHP is a server side language. It can't access php.

  • Related