Hướng dẫn php get tag content

"; $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 = "
"; $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

Hướng dẫn php get tag content

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 ?> and everything outside of that is ignored by the php parser.

php code..//parsed
php code..//parsed
?>
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.
//php code;
//php code;
//php code;
but if you are embedding php with html then enclose php code with opening and closing tag.
<
html>
<
head>
head>
<
body>
php
//php code;
//php code;
//php code;
?>

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.
        //$var = 3;
        //$var2 = 2;
        //$var3 = $var+$var2;
        //if($var3){//result}
?>

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







but if you have multiple line, then use semicolon.
//line 1;
//line 2;
//line 3;
?>

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

    echo 'htmlspecialchars($data) . '" />'."\n";
?>

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

    echo "\n";
    echo 
htmlspecialchars($data)."\n";
    echo 
"";
?>

Note: The data is shown in the browser as intended, because the browser will interpret the HTML escaped symbols. Upon submitting, either via GET or POST, the data will be urlencoded by the browser for transferring, and directly urldecoded by PHP. So in the end, you don't need to do any urlencoding/urldecoding yourself, everything is handled automagically.

Example #3 In a URL

    echo 'htmlspecialchars("/nextpage.php?stage=23&data=" .
        
urlencode($data)) . '">'."\n";
?>

Note: In fact you are faking a HTML GET request, therefore it's necessary to manually urlencode() the data.

Note: You need to htmlspecialchars() the whole URL, because the URL occurs as value of an HTML-attribute. In this case, the browser will first un-htmlspecialchars() the value, and then pass the URL on. PHP will understand the URL correctly, because you urlencode()d the data. You'll notice that the & in the URL is replaced by &. Although most browsers will recover if you forget this, this isn't always possible. So even if your URL is not dynamic, you need to htmlspecialchars() the URL.

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?

When submitting a form, it is possible to use an image instead of the standard submit button with a tag like:

When the user clicks somewhere on the image, the accompanying form will be transmitted to the server with two additional variables: foo.x and foo.y.

Because foo.x and foo.y would make invalid variable names in PHP, they are automagically converted to foo_x and foo_y. That is, the periods are replaced with underscores. So, you'd access these variables like any other described within the section on retrieving variables from external sources. For example, $_GET['foo_x'].

Note:

Spaces in request variable names are converted to underscores.

How do I create arrays in a HTML ?

To get your result sent as an array to your PHP script you name the ,

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
test1
Test2