Hướng dẫn php get tag content

Is it possible to get all HTML elements [children] with content using PHP [DOMDocument class]? I just can't get the results. Let say I only know that I will have tag but don't know what tags would be inside Example:

$doc = new DOMDocument[]; 
$el = "test1
Test2
"; $doc->loadHTML[$el]; $doc->getElementsByTagName["td"]->item[0]->nodeValue /* I only get plain text */

EDIT: No JavaScript like solution

asked Dec 6, 2021 at 12:27

SlitSlit

4811 gold badge6 silver badges20 bronze badges

2

This will give you all element information:

$html = "test1
Test2
"; $dom = new DOMDocument[]; $dom->loadHTML[$html]; foreach[$dom->getElementsByTagName['*'] as $element ]{ echo "
";
    print_R[$element];
    echo "
"; }

To get Attribute information use like:

$p = $dom->getElementsByTagName['a']->item[0];
if [$p->hasAttributes[]] {
  foreach [$p->attributes as $attr] {
    $name = $attr->nodeName;
    $value = $attr->nodeValue;
    echo "Attribute '$name' :: '$value'
"; } }

answered Dec 6, 2021 at 12:56

4

anisgazig at gmail dot com

10 months ago

If you want your file to be interpreted as php then your file must start and end with and everything outside of that is ignored by the php parser.


hellow..//normal test but ignred by php parser

Three types of tag are available in php
1.normal tag[]
2.short echo tag[]
3.short tag[]

short tag are bydefault available but can be disabled by short_open_tag = Off and also disabled bydefault if php will  built with --disabe--short--tags[]

As short tag can be disabled so only use the normal and short echo tag.

If your file only have php code then  do not use closing tag.


If you want to just print single text or something ,you should use shorthand version .

But if you want to process something, you should use normal tag.


If you embedded php with html and single line, do not need to use semicolon







but if you have multiple line, then use semicolon.

PHP and HTML interact a lot: PHP can generate HTML, and HTML can pass information to PHP. Before reading these faqs, it's important you learn how to retrieve variables from external sources. The manual page on this topic includes many examples as well.

  1. What encoding/decoding do I need when I pass a value through a form/URL?
  2. I'm trying to use an tag, but the $foo.x and $foo.y variables aren't available. $_GET['foo.x'] isn't existing either. Where are they?
  3. How do I create arrays in a HTML ?
  4. How do I get all the results from a select multiple HTML tag?
  5. How can I pass a variable from Javascript to PHP?

What encoding/decoding do I need when I pass a value through a form/URL?

There are several stages for which encoding is important. Assuming that you have a string $data, which contains the string you want to pass on in a non-encoded way, these are the relevant stages:

  • HTML interpretation. In order to specify a random string, you must include it in double quotes, and htmlspecialchars[] the whole value.

  • URL: A URL consists of several parts. If you want your data to be interpreted as one item, you must encode it with urlencode[].

Example #1 A hidden HTML form element

Note: It is wrong to urlencode[] $data, because it's the browsers responsibility to urlencode[] the data. All popular browsers do that correctly. Note that this will happen regardless of the method [i.e., GET or POST]. You'll only notice this in case of GET request though, because POST requests are usually hidden.

Example #2 Data to be edited by the user

Chủ Đề