PHP: strip_tags should replace with spaces
Question by Compi | 2016-09-23 at 17:26
I am using the PHP function strip_tags() for removing all HTML tags from a string.
Actually, this is working quite well, however, there are some little problems arising from strings like that:
<p>First Sentence.</p><p>Second Sentence.</p> <table> <tr><td>First Column</td><td>Second Column</td></tr> </table>
Namely, the function simply removes all tags with which the following result arises:
First Sentence.Second Sentence. First ColumnSecond Column
Of course, this is very inelegant, because after each paragraph or even after the end of each DIV container there is no distance between the closing punctuation marks. It is even worse with tables: here, after each tag deletion with strip_tags(), all of the characters and columns are sticked together without any spacing.
I would rather see the behavior that instead of just deleting the tags without taking into account any content, a space is inserted instead of the tag. So, I would like to have the following result from the example above:
First Sentence. Second Sentence. First Column Second Column
How is it possible to control strip_tags in a way that every tag is just replaced by a blank? When exporting the result as HTML, it would even be not a problem if there were multiple blanks after each other in the string, because there will be ignored in every browser.
Related Topics
PHP: Remove all HTML Tags from String
Tutorial | 0 Comments
PHP: Remove arbitrary Characters at the Beginning and the End of a String
Tutorial | 0 Comments
HTML: No line break between particular words
Tip | 0 Comments
PHP: Check Strings with Ctype-Functions for Character Classes
Article | 0 Comments
MySQL: Search and Replace directly in MySQL database
Tip | 1 Comment
Search and Replace in File Names
Tutorial | 0 Comments
Rename File to its Folder Name
Tutorial | 0 Comments
Important Note
Please note: The contributions published on askingbox.com are contributions of users and should not substitute professional advice. They are not verified by independents and do not necessarily reflect the opinion of askingbox.com. Learn more.
Participate
Ask your own question or write your own article on askingbox.com. That’s how it’s done.
Unfortunately, strip_tags() does not provide a parameter with which you can control the replacement.
However, you can just prepare your string before calling strip_tags(). This can look like that:
As you can see, before calling strip_tags(), we are replacing each occurrence of "><" by "> <". This should work independent from the actual tag and should replace a "</p><p>" in the same way as a "</td><td>" by "</p> <p>" respectively "</td><td>".
The result of strip_tags() then contains the spaces at the corresponding positions.
2016-09-23 at 23:14
Instead of using the function strip_tags(), you can also create your own regular expression for doing the job. Here is an example:
The second line replaces each tag with a blank, so that we have two blanks between "ABC" and "DEF" after that.
If we want to avoid this, we can use the third line for deleting all double spaces. Additionally, we can use trim() to cut off the blanks from the beginning and the end of the string before the output.
2016-09-24 at 14:28