Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to extract only text from HTML string with PHP?

Tags:

php

I want to extract only text from a php string.

This php string contains html code like tags or etc.

So I only need a simple text from this string.

This is the actual string:

<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 the string:

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…

Actually this text will be placed in meta description tag so I don't need any HTML in meta tag. How can I perform this? Any ideas and thoughts about this technique ?

like image 411
New Co Avatar asked Oct 13 '14 15:10

New Co


People also ask

How to extract substring from a string in PHP?

How to extract substring from a string in PHP. Answer: Use the PHP substr() function. The PHP substr() function can be used to get the substring i.e. the part of a string from a string. This function takes the start and length parameters to return the portion of string. Example. $str = "Hello World!";

How can I extract only text data from HTML pages?

Originally Answered: How I can extract only text data from HTML pages? You will have to a bit specific on the text part so as to which text exactly you want to fetch for example a div with some id etc, you can either use Php, it has file_get_contents () function or even jquery with the .text () function etc.

How to extract the last character of a string in PHP?

The last character in the input string has an index of -1. Use the negative length to omit a length number of characters in the returned substring. Use the PHP mb_substr () function to extract a substring from a string with non-ASCII characters.

How do I get the part of a string in PHP?

Answer: Use the PHP substr() function The PHP substr() function can be used to get the substring i.e. the part of a string from a string. This function takes the start and length parameters to return the portion of string.


2 Answers

You may try:

echo(strip_tags($your_string));

More info here: http://php.net/manual/en/function.strip-tags.php

like image 167
MillaresRoo Avatar answered Sep 30 '22 19:09

MillaresRoo


Another option is to use Html2Text. It will do a much better job than strip_tags, especially if you want to parse complicated HTML code.

Extracting text from HTML is tricky, so your best bet is to use a library built for this purpose.

https://github.com/mtibben/html2text

Install using composer:

composer require html2text/html2text

Basic usage:

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

echo $html->getText();  // Hello, "WORLD"
like image 32
Paulius Jacionis Avatar answered Sep 30 '22 20:09

Paulius Jacionis