php - How to remove <br /> tags and more from a string?Get the solution ↓↓↓
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
$description = preg_replace('<br />', '', $description);
But it returned
<> in place of every
<?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>'); ?>
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.
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;
A string with double quotes .
Please provide an example of your input string and what you expect it to look like after filtering.
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 /> with more than one space, or even
<BR/>, which altough illegal are sometimes used). See for example here: http://sourceforge.net/projects/simplehtmldom/
To remove all permutations of br:
<br> <br /> <br/> <br >
check out the user contributed
strip_only() function in
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.
$description = preg_replace('/<br \/>/iU', '', $description);
This worked for me, to remove
> is recognised whereas > isn't)
$temp2 = str_replace('<','', $temp); // echo ($temp2); $temp2 = str_replace('/>','', $temp2); // echo ($temp2); $temp2 = str_replace('br','', $temp2); echo ($temp2);
People are also looking for solutions of the problem: string literal contains an unescaped line break
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.