Skip to content

Instantly share code, notes, and snippets.

@code-boxx
Last active May 26, 2023 03:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save code-boxx/8b8198ca8f7d0d2861a212ebf6c8bdf9 to your computer and use it in GitHub Desktop.
Save code-boxx/8b8198ca8f7d0d2861a212ebf6c8bdf9 to your computer and use it in GitHub Desktop.
PHP Remove HTML Tags

REMOVE & STRIP HTML TAGS IN PHP MYSQL

https://code-boxx.com/remove-html-tags-php-mysql/

NOTES

  1. Create a test database and import 1-database.sql.
  2. Change the database settings in 2-remove-html.php to your own and launch it in the browser.
  3. Alternatively, import 3a-strip-tag.sql for the stored MySQL function and check out 3b-insert.sql.

LICENSE

Copyright by Code Boxx

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

CREATE TABLE `reviews` (
`review_id` bigint(20) NOT NULL,
`review_name` varchar(255) NOT NULL,
`review_text` text NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
ALTER TABLE `reviews`
ADD PRIMARY KEY (`review_id`);
ALTER TABLE `reviews`
MODIFY `review_id` bigint(20) NOT NULL AUTO_INCREMENT;
<?php
// (A) THE PROBLEMETIC REVIEW
$_POST = [
"name" => "Le Hackr",
"text" => "<strong>Good product!</strong> <p>Foo Bar</p>".
"<script>alert('POO PAR')</script>"
];
// (B) CONNECT TO DATABASE - CHANGE SETTINGS TO YOUR OWN !
$dbhost = "127.0.0.1";
$dbname = "test";
$dbuser = "root";
$dbpass = "";
$dbchar = "utf8mb4";
$pdo = new PDO(
"mysql:host=$dbhost;dbname=$dbname;charset=$dbchar",
$dbuser, $dbpass, [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC
]);
// (C) INSERT SQL
$stmt = $pdo->prepare("INSERT INTO `reviews` (`review_name`, `review_text`) VALUES (?,?)");
// (C1) STRIP ALL HTML TAGS
echo $stmt->execute([$_POST["name"], strip_tags($_POST["text"])])
? "OK" : "ERROR!" ;
// (C2) STRIP HTML TAGS (BUT SELECTIVELY ALLOW SOME)
echo $stmt->execute([$_POST["name"], strip_tags($_POST["text"], "<p><strong>")])
? "OK" : "ERROR!" ;
// (C3) ALLOW HTML BUT CONVERT TO HTML ENTITIES
echo $stmt->execute([$_POST["name"], htmlentities($_POST["text"]) ])
? "OK" : "ERROR!" ;
DELIMITER $$
CREATE FUNCTION `strip_tags`($str text)
RETURNS text
DETERMINISTIC
BEGIN
DECLARE $start, $end INT DEFAULT 1;
LOOP
SET $start = LOCATE("<", $str, $start);
IF (!$start) THEN RETURN $str; END IF;
SET $end = LOCATE(">", $str, $start);
IF (!$end) THEN SET $end = $start; END IF;
SET $str = INSERT($str, $start, $end - $start + 1, "");
END LOOP;
END$$
DELIMITER ;
INSERT INTO `reviews`
(`review_name`, `review_text`)
VALUES
('Jane Doe', strip_tags('Hello world <strong>foo</strong> bar'));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment