1
2// using strip_tags and str_replace for REMOVE all html TAGS in php
3$text = '<p>Hello world.</p><!-- Comment --> <a href="https://learn-tech-tips.blogspot.com">Zidane</a>';
4echo strip_tags($text);
5
6//Hello world. Zidane
7
8$short_description = strip_tags(str_replace(" ", " ", $short_description));
9echo $short_description
10
11
12// Allow <p> and <a>
13echo strip_tags($text, '<p><a>');
14
15// <p>Hello world.</p><a href="https://learn-tech-tips.blogspot.com">Zidane</a>
1/* EXAMPLE: <p>Bed & Breakfast</p> --> <p>Bed & Breakfast</p>
2 & &
3 " " (unless ENT_NOQUOTES is set)
4 ' ' or ' (ENT_QUOTES must be set)
5 < <
6 > > */
7
8<?php
9$new = htmlspecialchars("<a href='test'>Test</a>", ENT_QUOTES);
10echo $new; // <a href='test'>Test</a>
11?>
1<?php
2$text = '<p>Test paragraph.</p><!-- Comment --> <a href="#fragment">Other text</a>';
3echo strip_tags($text);
4//Test paragraph. Other text
5
6// Allow <p> and <a>
7echo strip_tags($text, '<p><a>');
8//<p>Test paragraph.</p> <a href="#fragment">Other text</a>
9// as of PHP 7.4.0 the line above can be written as:
10// echo strip_tags($text, ['p', 'a']);
11?>
12
13
1<?php
2$str = "<h1>Hello WorldÆØÅ!</h1>";
3# Remove all HTML tags and all characters with ASCII value > 127, from a string:
4$newstr = filter_var($str, FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_HIGH);
5echo $newstr;
6# Result: Hello World!