Wizard
Wizard

Reputation: 11265

PHP removing html tags from string

I have string:

<p justify;"="">Vers­lo cent­rai Lie­tu­vos ne­kil­no­ja­mo­jo turto plėt­ros aso­cia­ci­jos kon­kur­se  ...</p>

and want want remove tag

<p justify;"=""></p>

my code:

$content = strip_tags($text, '<p>');

but i get empty string: string(0) "" , what I do wrong ?

Upvotes: 12

Views: 46562

Answers (6)

Fellipe Sanches
Fellipe Sanches

Reputation: 8125

From PHP 7.4.0 the strip_tags() alternatively accepts an array with allowable tags,

then this:

<?php

$html = '<div id="my-div"><p>text<strong><a href="#link"></a></strong></p></div>';

echo strip_tags($html, ['p', 'a']); //accept p and a tags

Return this:

<p>text<a href="#link"></a></p>

Note that only the disallowed tags have been removed.

Upvotes: 1

APetrovsky
APetrovsky

Reputation: 31

This will replace all html tags, https://regex101.com/r/jM9oS4/4

preg_replace('/<(|\/)(?!\?).*?(|\/)>/',$replacement,$string);

Upvotes: 1

Wilf
Wilf

Reputation: 2315

This will remove every thing - tags, ascii, line breaks but pure text:

strip_tags(preg_replace('/<[^>]*>/','',str_replace(array("&nbsp;","\n","\r"),"",html_entity_decode($YOUR_STRING,ENT_QUOTES,'UTF-8'))));

Upvotes: 4

Toretto
Toretto

Reputation: 4711

Try to put it like that

$content = strip_tags($text);

Or you can do it with regular expression like that:

$content = preg_replace('/<[^>]*>/', '', $text);

By this $content = strip_tags($text, '<p>'); you are allowing the <p> tag in the string.

For more info see the link http://php.net/manual/en/function.strip-tags.php

Upvotes: 19

Mihai Iorga
Mihai Iorga

Reputation: 39704

Since your HTML is not properly formatted you could choose a preg_replace() approach:

$text = '<p justify;"="">Vers­lo cent­rai Lie­tu­vos ne­kil­no­ja­mo­jo turto plėt­ros aso­cia­ci­jos kon­kur­se ... </p>';
$content = preg_replace('/<[^>]*>/', '', $text); 
var_dump($content);
// string(108) "Vers­lo cent­rai Lie­tu­vos ne­kil­no­ja­mo­jo turto plėt­ros aso­cia­ci­jos kon­kur­se ... "

Codepad Example

On strip_tags() docs it says: Because strip_tags() does not actually validate the HTML, partial or broken tags can result in the removal of more text/data than expected.

Also second parameter is for $allowable_tags.

Upvotes: 4

Magnus Lindgren
Magnus Lindgren

Reputation: 287

Since the HTML is poorly formated you probably need to either write your own regexp to remove tags or clean up the HTML before trying to remove tags.

You could try this to remove everything that "looks like" a tag:

$str = preg_replace("/<.*?>/", " ", $str);

Upvotes: 5

Related Questions