21

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 ?

2
  • 1
    remove all html tags with strip_tags() Commented Oct 13, 2014 at 15:35
  • 1
    use strip_tags(), check the manual and you'll get the details on how you will use it and what it does. Commented Oct 13, 2014 at 15:40

3 Answers 3

77

You may try:

echo(strip_tags($your_string));

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

Sign up to request clarification or add additional context in comments.

3 Comments

isn't there any way we could remove html characters
<style>lots of css</style> will mess this up. along with many other tags that contain text you wouldn't want
11

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"

1 Comment

Warning: this library does NOT return text from meta tags.
1

Adding another option for someone else who may need this, the Stringizer library might be an option, see Strip Tags.

Full disclosure I'm the owner of the project.

1 Comment

cool project, although internally uses strip_tags() You have any idea of making a function that deals with inline css styling?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.