Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

escaping html tags and reading only the text in php?

Tags:

html

php

escaping

I was wondering is there a way to escape all html tags from a a bunch of html code, and extract only the text i.e.

<strong>this is strong</strong>
<br />
<h1> this is a header</h1>

and I want to get the text between the tags only, so basically only this is strong this is a header

like image 654
getaway Avatar asked Dec 13 '22 18:12

getaway


2 Answers

$text = '<p>Test paragraph.</p><!-- Comment --> <a href="#fragment">Other text</a>';
echo strip_tags($text);

One of the things I love about PHP...thoughtful functions for web developers.

like image 167
clifgriffin Avatar answered Jan 01 '23 14:01

clifgriffin


Use strip_tags() like this:

$html = "<strong>this is strong</strong><br />\n".
        "<h1> this is a header</h1>";
$text = strip_tags($html);
like image 34
Kelstar Avatar answered Jan 01 '23 15:01

Kelstar