Untitled Document

PHP — How I can get innerHTML on an element?

Question: I have some code for get html table result tracking from this site http://www.jne.co.id/tarif.php i try to create from my localhost this code: i need to get table html with class»table_style tracking», see this image Image EDIT: i just need get html with class tracking, not string inside html. Question: I’m currently using nodeValue to give me HTML output, however it is stripping the HTML code and just giving me plain text.

PHP — How I can get innerHTML on an element?

I am novice in PHP. I have javascript functions for adding , removing and moving favorites (DOM Manipulation only). When I refresh the page this favorites are cleared. I need:

1.Get innerHTML on current (DOM manipulated) favorites DIV element (where favorites are).

2.Store the HTML in PHP varable (i need ‘php’ varable cuz i will insert the value in DB).

Читайте также:  Java web application security

Favorite DIV with one favorite created :

 
Zamunda.NET

I would do one of the following, depending on what you’re really trying to accomplish.

  1. Do an ajax post to your PHP code that contains the information required to save the favorite entries (i.e. just the ID, URL and name, not the actual HTML).
  2. Maintain a few hidden form fields that hold the information required to save the favorite entries. This will give you access to the data from PHP with the form is submitted (assuming a form is involved).

Note that I don’t think what you really want is to get the innerHTML of elements from your PHP code. You probably want to find a way to represent that information in a way that can more easily be sent to your server-side code.

var v = document.getElementsByClassName('minID')[0].innerHTML; // select whatever $.cookie("something", v, < expires: 5 >); // set cookie 

Html — Using PHP to get DOM Element, 2 Answers Sorted by: 22 getElementsByTagName returns you a list of elements, so first you need to loop through the …

How to extract innerHTML using the PHP Dom [duplicate]

I’m currently using nodeValue to give me HTML output, however it is stripping the HTML code and just giving me plain text. Does anyone know how I can modify my code to give me the inner HTML of an element by using it’s ID?

function getContent($url, $id)< // This first section gets the HTML stuff using a URL $ch = curl_init($url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_BINARYTRANSFER, true); $html = curl_exec($ch); curl_close($ch); // This second section analyses the HTML and outputs it $newDom = new domDocument; $newDom->loadHTML($html); $newDom->preserveWhiteSpace = false; $newDom->validateOnParse = true; $sections = $newDom->getElementById($id)->nodeValue; echo $sections; > 
$sections = $newDom->saveXML($newDom->getElementById($id)); 

If you have PHP 5.3.6, this might also be an option:

$sections = $newDom->saveHTML($newDom->getElementById($id)); 

I have modify the code, and it’s working fine for me. Please find below the code

 $ch = curl_init($url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_BINARYTRANSFER, true); $html = curl_exec($ch); curl_close($ch); $newDom = new domDocument; libxml_use_internal_errors(true); $newDom->loadHTML($html); libxml_use_internal_errors(false); $newDom->preserveWhiteSpace = false; $newDom->validateOnParse = true; $sections = $newDom->saveHTML($newDom->getElementById('colophon')); echo $sections; 

Dom — How to get innerHTML of each element in a node, document |> Document.querySelectorAll(«.item») |> NodeList.toArray |> Array.iter(node => node |> Element.ofNode |> Js.Option.getExn |> Element.innerHTML |> Js.log ); It also has to be said that direct DOM manipulation isn’t a great fit for Reason, or any statically typed functional language for that …

Getting the Inner HTML of a DomElement in PHP [duplicate]

What’s the simplest way to get the innerHTML (tags and all) of a DOMElement using PHP’s DOM functions?

$html = ''; foreach($parentElement->childNodes as $node) < $html .= $dom->saveHTML($node); > 
Inner HTML

Try approach suggested by @trincot:

$html = implode(array_map([$node->ownerDocument,»saveHTML»], iterator_to_array($node->childNodes)));

Outer HTML
$html = $node->ownerDocument->saveHTML($node); 
$html = $node->ownerDocument->saveXML($node); 

Php — How to get innerHTML of DOMNode?, What function do you use to get innerHTML of a given DOMNode in the PHP DOM implementation? Can someone give reliable solution? Of course outerHTML will do too. Stack Overflow. About ; Products For Teams; Stack Overflow Public questions & answers; Stack Overflow for Teams Where …

How to use simple html dom get inner html [duplicate]

I have some code for get html table result tracking from this site http://www.jne.co.id/tarif.php

i try to create from my localhost this code:

 urlencode($_POST['origin_code']), 'dest_code' => urlencode($_POST['dest_code']), 'weight' => urlencode($_POST['weight']), 'g-recaptcha-response' => urlencode($_POST['g-recaptcha-response']), ); $fields_string = ''; //url-ify the data for the POST foreach($fields as $key=>$value) < $fields_string .= $key.'='.$value.'&'; >rtrim($fields_string, '&'); //open connection $ch = curl_init(); //set the url, number of POST vars, POST data curl_setopt($ch,CURLOPT_URL, $url); curl_setopt($ch,CURLOPT_POST, count($fields)); curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //execute post $result = curl_exec($ch); $string = $result;//htmlentities($result); //close connection curl_close($ch); // Create a DOM object $html_base = new simple_html_dom(); // Load HTML from a string $html_base->load($string); $str = $html_base->find('.tracking'); echo $str; $html_base->clear(); unset($html_base); > ?>       

Origin Shipment

i need to get table html with class»table_style tracking», see this image

 
Nama Layanan Jenis Kiriman Tarif ETD(Estimates Days)
OKE Dokumen / Paket Rp. 10.000 2 - 3 D
REG Dokumen / Paket Rp. 11.000 1 - 2 D
YES Dokumen / Paket Rp. 22.000 1 - 1 D
SPS Dokumen / Paket Rp. 403.000
$items = str_get_html($html); $count = 0; $country = $items->find('.category-label span', 0)->text(); // first element by index 0 

How to extract innerHTML using the PHP Dom, Browse other questions tagged php dom innerhtml nodevalue or ask your own question. The Overflow Blog Skills that pay the bills for software developers (Ep. 460)

Источник

php — How to get innerHTML of DOMNode?

What function do you use to get innerHTML of a given DOMNode in the PHP DOM implementation? Can someone give reliable solution?

Of course outerHTML will do too.

Answer

Solution:

childNodes; foreach ($children as $child) < $innerHTML .= $element->ownerDocument->saveHTML($child); > return $innerHTML; > ?> 
preserveWhiteSpace = false; $dom->formatOutput = true; $dom->load($html_string); $domTables = $dom->getElementsByTagName("table"); // Iterate over DOMNodeList (Implements Traversable) foreach ($domTables as $table) < echo DOMinnerHTML($table); >?> 

Answer

Solution:

Here is a version in a functional programming style:

function innerHTML($node) < return implode(array_map([$node->ownerDocument,"saveHTML"], iterator_to_array($node->childNodes))); > 

Answer

Solution:

To return the html of an element, you can use C14N():

$dom = new DOMDocument(); $dom->loadHtml($html); $x = new DOMXpath($dom); foreach($x->query('//table') as $table)< echo $table->C14N(); > 

Answer

Solution:

A simplified version of Haim Evgi’s answer:

ownerDocument; $html = ''; foreach ($element->childNodes as $node) < $html .= $doc->saveHTML($node); > return $html; > 
loadHTML("

This is an example paragraph
\n\ncontaining newlines.

This is another paragraph.

"); print innerHTML($doc->getElementById('foo')); /*

This is an example paragraph
containing newlines.

This is another paragraph.

*/

There’s no need to set preserveWhiteSpace or formatOutput .

Answer

Solution:

In addition to trincot’s nice version with array_map and implode but this time with array_reduce :

return array_reduce( iterator_to_array($node->childNodes), function ($carry, \DOMNode $child) < return $carry.$child->ownerDocument->saveHTML($child); > ); 

Still don’t understand, why there’s no reduce() method which accepts arrays and iterators alike.

Answer

Solution:

function setnodevalue($doc, $node, $newvalue)< while($node->childNodes->length> 0)< $node->removeChild($node->firstChild); > $fragment= $doc->createDocumentFragment(); $fragment->preserveWhiteSpace= false; if(!empty($newvalue))< $fragment->appendXML(trim($newvalue)); $nod= $doc->importNode($fragment, true); $node->appendChild($nod); > > 

Answer

Solution:

Here’s another approach based on this comment by Drupella on php.net, that worked well for my project. It defines the innerHTML() by creating a new DOMDocument , importing and appending to it the target node, instead of explicitly iterating over child nodes.

InnerHTML

Let’s define this helper function:

function innerHTML( \DOMNode $n, $include_target_tag = true ) < $doc = new \DOMDocument(); $doc->appendChild( $doc->importNode( $n, true ) ); $html = trim( $doc->saveHTML() ); if ( $include_target_tag ) < return $html; >return preg_replace( '@^nodeName .'[^>]*>|nodeName .'>$@', '', $html ); > 

where we can include/exclude the outer target tag through the second input argument.

Usage Example

Here we extract the inner HTML for a target tag given by the «first» id attribute:

$html = '

Hello

World!

'; $doc = new \DOMDocument(); $doc->loadHTML( $html ); $node = $doc->getElementById( 'first' ); if ( $node instanceof \DOMNode ) < echo innerHTML( $node, true ); // Output:

Hello

echo innerHTML( $node, false ); // Output:

Hello

>

Answer

Solution:

Old query, but there is a built-in method to do that. Just pass the target node to DomDocument->saveHtml() .

$html = '

ciao questa ГЁ una prova.

'; $dom = new DomDocument($html); @$dom->loadHTML($html); $xpath = new DOMXPath($dom); $node = $xpath->query('.//div/*'); // with * you get inner html without surrounding div tag; without * you get inner html with surrounding div tag $innerHtml = $dom->saveHtml($node); var_dump($innerHtml);

Output:

ciao questa ГЁ una prova.

Answer

Solution:

For people who want to get the HTML from XPath query, here is my version:

$xpath = new DOMXpath( $my_dom_object ); $DOMNodeList = $xpath->query('//div[contains(@class, "some_custom_class_in_html")]'); if( $DOMNodeList->count() > 0 ) < $page_html = $my_dom_object->saveHTML( $DOMNodeList->item(0) ); > 

Share solution ↓

Additional Information:

Didn’t find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Similar questions

Find the answer in similar questions on our website.

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

About the technologies asked in this question

PHP

PHP (from the English Hypertext Preprocessor — hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites. The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
https://www.php.net/

HTML

HTML (English «hyper text markup language» — hypertext markup language) is a special markup language that is used to create sites on the Internet. Browsers understand html perfectly and can interpret it in an understandable way. In general, any page on the site is html-code, which the browser translates into a user-friendly form. By the way, the code of any page is available to everyone.
https://www.w3.org/html/

Welcome to programmierfrage.com

programmierfrage.com is a question and answer site for professional web developers, programming enthusiasts and website builders. Site created and operated by the community. Together with you, we create a free library of detailed answers to any question on programming, web development, website creation and website administration.

Get answers to specific questions

Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.

Help Others Solve Their Issues

Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.

Источник

Оцените статью