Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sql query to convert new line character to a html <br>

I have a mysql database of articles that were entered into a textarea with no formatting, they use only a newline character for line breaks

\n

I need to convert all of these into html br tags

<br />

can you help me write a query to do this that I can run in phpmyadmin that will do this?

the name of the table is

exp_channel_data

as a bonus question...

Is there a query I can run that will strip everything out of the middle of p and span tags

I want to get rid of this stuff

<p class="MsoNormal" style="MARGIN: 0in 0in 0pt">
<span face="Times New Roman">

and end up with just this

<p>
<span>
like image 748
mjr Avatar asked Aug 24 '11 14:08

mjr


People also ask

How do you handle a new line character in SQL?

SQL Server ' AS 'New Line' -- using carriage return: CHAR(13) SELECT 'First line. '+ CHAR(13) + 'Second line. ' AS 'New Line' -- Using both: CHAR(13)+CHAR(10) SELECT 'First line. '+ CHAR(13)+CHAR(10) + 'Second line.

What does like %% mean in SQL?

The LIKE operator is used in a WHERE clause to search for a specified pattern in a column. There are two wildcards often used in conjunction with the LIKE operator: The percent sign (%) represents zero, one, or multiple characters. The underscore sign (_) represents one, single character.

What is CR LF in SQL?

CRLF (Carriage Return and Line Feed)f in a SQL statement is replaced by just a LF.

How remove CR LF in SQL?

Remove and Replace Carriage Returns and Line Breaks in SQL Using SQL to remove a line feed or carriage return means using the CHAR function. A line feed is CHAR(10); a carriage return is CHAR(13).


2 Answers

First question:

UPDATE exp_channel_data SET text_column = REPLACE(text_column, '\r\n', '<br />')

If it doesn't replace anything, use '\n' instead of '\r\n'.

Second question:

You can't do it with a SQL query, you have to fetch this data into a PHP script, or anything else you like, and perform a regular expression replace (example for PHP):

$new_str = preg_replace('#<(p|span)[^>]+>#', '<$1>', $old_string);
like image 120
piotrp Avatar answered Oct 08 '22 10:10

piotrp


UPDATE yourTable SET text=REPLACE(text,"\n","<br />")

This works for your first question.

like image 37
Jacob Avatar answered Oct 08 '22 10:10

Jacob