I would like to use Simple HTML DOM to remove all images in an article so I can easily create a small snippet of text for a news ticker but I haven't figured out how to remove elements with it.
Basically I would do
- Get content as HTML string
- Remove all image tags from content
- Limit content to x words
- Output.
Any help?
This is working for me:
The supposed solutions are quite expensive and practically unusable in a big loop or other kind of repetition.
I prefer to use "soft deletes":
There is no dedicated methods for removing elements. You just find all the img elements and then do
when you only delete the outer text you delete the HTML content itself, but if you perform another find on the same elements it will appear in the result. the reason is that the simple HTML DOM object still has it's internal structure of the element, only without its actual content. what you need to do in order to really delete the element is simply reload the HTML as string to the same variable. this way the object will be recreated without the deleted content, and the simple HTML DOM object will be built without it.
here is an example function:
put this function inside the simple_html_dom class and you're good.
I could not figure out where to put the function so I just put the following directly in my code:
It basically locks changes made in the for loop back into the html per above.
I think you have some difficulties because you forgot to save(dump the internal DOM tree back into string).
Try this: