MarathonStudios
MarathonStudios

Reputation: 4321

How do you replace < and > (but not what's between them) with PHP RegExp?

I've found dozens of functions that can remove whole HTML tags with the preg_replace() function, but I need to remove only the html brackets < and > (leaving anything inside them put). What regexp would accomplish this?

Upvotes: 1

Views: 80

Answers (2)

NikiC
NikiC

Reputation: 101936

How about htmlspecialchars. It replaces HTML reserved characters with escape sequences. This way the characters are displayed in the browser but aren't harmful at all.

This probably is what you want - show exactly the text the client typed in, but make it harmless.

PS: If you really do need a regex to remove tag-brackets, here you go: $text = preg_replace('/[<>]/', '', $text).

Upvotes: 3

Gabi Purcaru
Gabi Purcaru

Reputation: 31524

How about

str_replace(array("<", ">", "&lt;", "&gt;"), "", $text);

..?

Upvotes: 0

Related Questions