user188962
user188962

Reputation:

How to remove <br /> tags and more from a string?

I need to strip all <br /> and all 'quotes' (") and all 'ands' (&) and replace them with a space only ...

How can I do this? (in PHP)

I have tried this for the <br />:

   $description = preg_replace('<br />', '', $description);

But it returned <> in place of every <br />...

Thanks

Upvotes: 4

Views: 64791

Answers (8)

Vitor Maia
Vitor Maia

Reputation: 1

$string = "Test<br>Test<br />Test<br/>";
$string = preg_replace( "/<br>|\n|<br( ?)\/>/", " ", $string );
echo $string;

Upvotes: 0

pete
pete

Reputation: 1

This worked for me, to remove <br/> :

(&gt; is recognised whereas > isn't)

$temp2 = str_replace('&lt;','', $temp);

// echo ($temp2);

$temp2 = str_replace('/&gt;','', $temp2);

// echo ($temp2);

$temp2 = str_replace('br','', $temp2);

echo ($temp2);

Upvotes: -1

Thevsuman
Thevsuman

Reputation: 101

<?php

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

?>

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

Upvotes: 10

Art3mk4
Art3mk4

Reputation: 439

Try this:

$description = preg_replace('/<br \/>/iU', '', $description);

Upvotes: 1

Gordon
Gordon

Reputation: 316969

If str_replace() isnt working for you, then something else must be wrong, because

$string = 'A string with <br/> & "double quotes".';
$string = str_replace(array('<br/>', '&', '"'), ' ', $string);
echo $string;

outputs

A string with      double quotes .

Please provide an example of your input string and what you expect it to look like after filtering.

Upvotes: 5

Pekka
Pekka

Reputation: 449395

To remove all permutations of br:

<br> <br /> <br/> <br   >

check out the user contributed strip_only() function in

http://www.php.net/strip_tags

The "Use the DOM instead of replacing" caveat is always correct, but if the task is really limited to these three characters, this should be o.k.

Upvotes: 4

Konamiman
Konamiman

Reputation: 50273

To manipulate HTML it is generally a good idea to use a DOM aware tool instead of plain text manipulation tools (think for example what will happen if you enounter variants like <br/>, <br /> with more than one space, or even <br> or <BR/>, which altough illegal are sometimes used). See for example here: http://sourceforge.net/projects/simplehtmldom/

Upvotes: 4

Vincent Ramdhanie
Vincent Ramdhanie

Reputation: 103135

You can use str_replace like this:

  str_replace("<br/>", " ", $orig );

preg_replace etc uses regular expressions and that may not be what you want.

Upvotes: 7

Related Questions