How to extract only text from HTML string with PHP?

I want to extract only text from php string.

This php line contains html code like tags etc.

I need only plain text from this line.

This is the actual line:

<div class="devblog-index-content battlelog-wordpress">
<p><strong>The celebration of the Recon class in our second </strong><a href="http://blogs.battlefield.com/2014/10/bf4-class-week-recon/" target="_blank">BF4 Class Week</a><strong> continues with a sneaky stroll down memory lane. Learn more about how the Recon has changed in appearance, name and weaponry over the years&hellip;</strong></p>

<p>&nbsp;</p>

<p style="text-align:center"><a href="http://eaassets-a.akamaihd.net/battlelog/prod/954660ddbe53df808c23a0ba948e7971/en_US/blog/wp-content/uploads/2014/10/bf4-history-of-recon-1.jpg?v=1412871863.37"><img alt="bf4-history-of-recon-1" class="aligncenter" src="http://eaassets-a.akamaihd.net/battlelog/prod/954660ddbe53df808c23a0ba948e7971/en_US/blog/wp-content/uploads/2014/10/bf4-history-of-recon-1.jpg?v=1412871863.37" style="width:619px" /></a></p>

I want to show this from a line:

The celebration of the Recon class in our second BF4 Class Week continues with a sneaky stroll down memory lane. Learn more about how the Recon has changed in appearance, name and weaponry over the years…

In fact, this text will be placed in the meta description tag, so I do not need the HTML code in the meta tag. How can i do this? Any ideas or thoughts on this technique?

+4
source share
4 answers

You can try:

echo(strip_tags($your_string));

Further information here: http://php.net/manual/en/function.strip-tags.php

+23
source

- Html2Text. , strip_tags, HTML-.

HTML , , .

https://github.com/mtibben/html2text

:

composer require html2text/html2text

:

$html = new \Html2Text\Html2Text('Hello, &quot;<b>world</b>&quot;');

echo $html->getText();  // Hello, "WORLD"
+1

Adding another option for someone who might need it might be a Stringizer option , see Separator Tags .

Full disclosure. I am the owner of the project.

+1
source

Try it;

echo preg_replace("/<(.*?)>/", "", $string);

Where $stringis the line!

-2
source

All Articles