Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP removing html tags from string

Tags:

html

php

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 ?

like image 659
Wizard Avatar asked Mar 09 '13 09:03

Wizard


3 Answers

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

like image 123
Toretto Avatar answered Oct 19 '22 03:10

Toretto


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.

like image 32
Mihai Iorga Avatar answered Oct 19 '22 02:10

Mihai Iorga


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);
like image 39
Magnus Lindgren Avatar answered Oct 19 '22 04:10

Magnus Lindgren