2

Probably based on this question: Best way to strip html tags from a string in sql server?

Think of me as a noob. I have a field with data in it. I need to strip html from this column and I'm aware that there is a PHP function that does this. What do I do to do this in my SQL database?

What do I enter for the query? Select my column (the field I call it) from the table...and then?

9
  • You select the column from the table preferable along with an id of some sort. Then clean them and then update them, this is where the id comes in handy. So you will know you are updating the correct row. Commented Aug 29, 2014 at 12:17
  • Isn't it easier to strip HTML before you place things in your database? Commented Aug 29, 2014 at 12:18
  • I assume you're not using SQL-Server, because that other question explains how to do it there. What DBMS are you using? Commented Aug 29, 2014 at 12:20
  • So you want to strip html from database without PHP but strictly by sql? that is because you're aware of the strip_tags() function. Commented Aug 29, 2014 at 12:26
  • I want to use PHP but dont know wher it go Commented Aug 29, 2014 at 12:38

2 Answers 2

0

Using strip_tag() function

// assuming that you're using MySQL
$result = mysqli_query("SELECT column FROM table");
while ($row = mysqli_fetch_array($result)) {
  echo strip_tags($row['column']) . '<br />';
}

or using preg_replace() function and this simple regular expression <[^>]*>

echo preg_replace('/<[^>]*>/', '', $row['column']);

Working PHP demo

Recomended method: strip_tags() function in this case. It takes two arguments; the first one is the text itself and the second one contains allowed tags; so, for instance, if you want to keep <a> and clean everything else you simply use it like this:

strip_tags($text, '<a>');
Sign up to request clarification or add additional context in comments.

Comments

0

i am not a MS SQL user, but as i have seen the link you posted. its all explained there. All you need is to create a function by copy pasting that code.

//Function From the Link Given in Question

CREATE FUNCTION [dbo].[udf_StripHTML]
(@HTMLText VARCHAR(MAX))
RETURNS VARCHAR(MAX)
AS
BEGIN
DECLARE @Start INT
DECLARE @End INT
DECLARE @Length INT
SET @Start = CHARINDEX('<',@HTMLText)
SET @End = CHARINDEX('>',@HTMLText,CHARINDEX('<',@HTMLText))
SET @Length = (@End - @Start) + 1
WHILE @Start > 0
AND @End > 0
AND @Length > 0
BEGIN
SET @HTMLText = STUFF(@HTMLText,@Start,@Length,'')
SET @Start = CHARINDEX('<',@HTMLText)
SET @End = CHARINDEX('>',@HTMLText,CHARINDEX('<',@HTMLText))
SET @Length = (@End - @Start) + 1
END
RETURN LTRIM(RTRIM(@HTMLText))
END
GO

and i guess you can use query like this using the function above.

select * from table where dbo.udf_StripHTML(Content1) like ‘%Hello%’

im not sure about the above query, but i think it would be something like that. However the above function defined, just try to use in query, may be it solves your problem.

2 Comments

Thanks, but paste this where?
@ObiWanKobi somewhere in your code.. where it's above the query because if you go use functions you haven't defined yet you're gonna have a bad time.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.