How to Extract Only Text From HTML String With PHP

HTML (Hypertext Markup Language) is the standard markup language used for creating web pages.

However, sometimes, you might want to extract only the text content from an HTML string, without any HTML tags.

In such scenarios, PHP provides a simple and effective solution to extract only the text content.

In this tutorial, we’ll go over the steps to extract text from an HTML string using PHP.


Prerequisites

  • A basic understanding of HTML and PHP
  • A text editor and a PHP environment

Extracting Text from an HTML String

There are several ways to extract text from an HTML string in PHP.

In this section, we’ll go over the two most commonly used methods:

Using strip_tags()

The strip_tags() function is the simplest and quickest way to extract text from an HTML string.

This function removes all HTML and PHP tags from a string and returns only the plain text content.

Here’s an example:

$html = "<p>Hello, World!</p>";
$text = strip_tags($html);

echo $text; // outputs "Hello, World!"

Using Regular Expressions

Another way to extract text from an HTML string is by using regular expressions.

Regular expressions are a powerful tool that allows you to match patterns in strings and extract the desired parts.

Here’s an example:

$html = "<p>Hello, World!</p>";
$text = preg_replace("/<[^>]+>/", "", $html);

echo $text; // outputs "Hello, World!"

The regular expression used in the example above matches all HTML tags and replaces them with an empty string, effectively removing the tags and leaving only the text content.


Conclusion

Extracting text from an HTML string in PHP is a straightforward task that can be accomplished using either the strip_tags() function or regular expressions.

In this tutorial, we’ve gone over the steps to extract text from an HTML string using these two methods.

With this knowledge, you can easily extract text from HTML strings in your PHP projects.