Skip to content

How to get Node contents #995

Answered by colinodell
natanfelles asked this question in Q&A
Discussion options

You must be logged in to vote

Hi there!

TL;DR: Use $content = StringContainerHelper::getChildText($paragraph);

Paragraph nodes do not directly contain text, but some of their descendants do. Here's an example of such an AST (visualized as XML):

<document>
  <heading level="1">
    <text>Hello World!</text>
  </heading>
  <paragraph>
    <text>This is an example of </text>
    <strong>
      <text>CommonMark</text>
    </strong>
    <text>.</text>
  </paragraph>
</document>

To find that text, you'd need to iterate through those nodes, looking for any that implement StringContainerInterface, and concatenating what those contain.

Or you can use this helper function which does exactly that for you :)

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@natanfelles
Comment options

Answer selected by natanfelles
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants