Jump to content

How to select HTML tags inside textarea


lele_ballack
 Share

Recommended Posts

Hi everyone, 

I'm trying to create an article index using all the heading titles of an article, but haven't succeeded so far. 

I tried with: 

foreach ($page->text->h2 as $heading)

But I get this:

"Notice: Trying to get property 'h2' of non-object"

What's the best way to get all the h2 tags inside the textarea?

I'm using CKEditor to write the articles. 

Thank you! 

Link to comment
Share on other sites

1 hour ago, lele_ballack said:

foreach ($page->text->h2 as $heading)

Your text is a string, not an object That's what the error is about.

1 hour ago, lele_ballack said:

What's the best way to get all the h2 tags inside the textarea?

You can use PHP's DOMDocument Class and the method getElementsByTagName(). Here's a tutorial (with a h2 example :-)):

https://codingreflections.com/php-parse-html/

I am not sure about performance issues with the class if it has to parse lots of text/HTML.

Edited by kongondo
  • Like 2
Link to comment
Share on other sites

16 hours ago, lele_ballack said:

this solution works like a charm! 

Excellent! Glad it worked. By the way, if your articles don't change often, you could cache the results of DOMDocument operations so that subsequent article index are fetched from the cache instead. Whenever an article changed, you would call DOMDocument again and then cache the new output. Have a look at WireCache here if this might be of interest:

https://processwire.com/api/ref/wire-cache/

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...