Using PHP to fetch content from a DOM node but not from its children

Mark

Is it possible to use PHP's DOMXPath (or a similar alternative) to extract the value bar but not the value foo from the following structure?

<div>
    <p><span>foo</span>bar</p>
</div>

All of my attempts so far have returned content from the node's descendants as well as from the node itself, which is not what I want in this case.

lonesomeday

The simple way is just to look for text nodes that are direct children of p:

$nodes = $xpath->query('//div/p/text()');

You may want a different selector in place of //div/, but the key bit is p/text(). / means "direct child nodes only" and text() means "text nodes only". So together they mean "direct children that are text nodes".

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

.children().innerHtml on a JQuery object from a DOM node object returns undefined

From Dev

PHP/SQL doesn't fetch all children from parent

From Dev

Fetch all children from database

From Dev

DomCrawler Symfony: how to get content from a node excluding children?

From Dev

AngularJS: Removing HTML element from DOM, but not its content

From Dev

Separate Content from Presentation using Shadow DOM

From Dev

php: dom replace node by root node from another dom

From Dev

Create array from DOM node values in PHP

From Dev

How do I extract an icon class name and its content value from a css file using PHP?

From Dev

How to fetch data from ajax.inc.php page, using node modules?

From Dev

Remove current node from HTML and fetch the final HTML using DOMDocument php

From Java

Failed to fetch tabular content from a webpage using requests

From Dev

Fetch children data from Firebase Database in ListView

From Dev

Select node from children by attribute

From Dev

Removing node and its children from d3js tree layout

From Dev

Using image from sprite in list item after its' content

From Dev

Access Toolbar (and its children) from fragment?

From Dev

IOS How to access uipageviewcontroller from its children?

From Dev

Get file content from multiple web pages using Dom Xpath

From Dev

How to fetch data from mongodb and show using node.js

From Dev

Fetch image from mysql database using node.js

From Java

Get Children PID using Parent PID from PHP

From Dev

How to get a specific child from an XML file and show its children on a div with PHP

From Dev

Fetch mysql data from PHP using radiobutton control

From Dev

Fetch Invoices from Quickbooks (with the help of API) using PHP

From Dev

how to fetch data from mysql database using php

From Dev

Fetch value from text file using preg_match PHP

From Dev

fetch id from one table and store it into another using php

From Dev

fetch same elements but different value from XML File using PHP

Related Related

  1. 1

    .children().innerHtml on a JQuery object from a DOM node object returns undefined

  2. 2

    PHP/SQL doesn't fetch all children from parent

  3. 3

    Fetch all children from database

  4. 4

    DomCrawler Symfony: how to get content from a node excluding children?

  5. 5

    AngularJS: Removing HTML element from DOM, but not its content

  6. 6

    Separate Content from Presentation using Shadow DOM

  7. 7

    php: dom replace node by root node from another dom

  8. 8

    Create array from DOM node values in PHP

  9. 9

    How do I extract an icon class name and its content value from a css file using PHP?

  10. 10

    How to fetch data from ajax.inc.php page, using node modules?

  11. 11

    Remove current node from HTML and fetch the final HTML using DOMDocument php

  12. 12

    Failed to fetch tabular content from a webpage using requests

  13. 13

    Fetch children data from Firebase Database in ListView

  14. 14

    Select node from children by attribute

  15. 15

    Removing node and its children from d3js tree layout

  16. 16

    Using image from sprite in list item after its' content

  17. 17

    Access Toolbar (and its children) from fragment?

  18. 18

    IOS How to access uipageviewcontroller from its children?

  19. 19

    Get file content from multiple web pages using Dom Xpath

  20. 20

    How to fetch data from mongodb and show using node.js

  21. 21

    Fetch image from mysql database using node.js

  22. 22

    Get Children PID using Parent PID from PHP

  23. 23

    How to get a specific child from an XML file and show its children on a div with PHP

  24. 24

    Fetch mysql data from PHP using radiobutton control

  25. 25

    Fetch Invoices from Quickbooks (with the help of API) using PHP

  26. 26

    how to fetch data from mysql database using php

  27. 27

    Fetch value from text file using preg_match PHP

  28. 28

    fetch id from one table and store it into another using php

  29. 29

    fetch same elements but different value from XML File using PHP

HotTag

Archive