I
I
Ivan2016-09-16 10:21:25
PHP
Ivan, 2016-09-16 10:21:25

How to extract all bare text from HTML using PHP?

At the input, HTML code with a bunch of tags and texts between them, there can also be tags inside the text.
The task is to get an array of strings with pieces of text without tags.
For example, if in HTML such code

<div>Здесь какой то текст <a href="#">А здесь ссылка</a> а дальше еще какой то текст</div>

Then there will be three lines in the array:
Here is some text
And here is a link
and then some more text
How to do this, maybe there is a regular expression for pulling out lines without tags?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
X
xmoonlight, 2016-09-16
@iwqn

$content=preg_replace('/<[^>]+>/', '', $content);

preg_match('/(?!<[^>]+>)/', $content); //собрать все строки в массив

A
Alexey Ukolov, 2016-09-16
@alexey-m-ukolov

strip_tags()

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question