Turn Excel line break into <br>
Asked Answered
R

3

10

A client sent me a huge list of product name and descriptions. The Description cells have text wrap and many line breaks. I need to import this into a MySQL which I do through Navicat Premium.

The problem is that the description cell is used as the HTML description of each product page.

Is there a way to replace Excel's line break with the <br> either in the same Excel file or by a php function?

Ridgepole answered 24/5, 2011 at 20:25 Comment(0)
L
18

A little bit of ASCII coding will go a long way.

Set up the find/replace dialogue (Ctrl-H). In the Find field, hold down the Alt key and type 010 from the numeric key pad. (This lets you find a linefeed character.) In the replace field, put your <br>.

Loire answered 24/5, 2011 at 20:47 Comment(1)
@EriF89 - In Excel's F&R you can also use Ctrl+J to get a linefeed (vbLF) character.Facsimile
B
5

or use a VBA function to replace the carriage returns in a string

Insert a MODULE and paste this

Function LineFeedReplace(ByVal str As String)
dim strReplace as String

strReplace = "<br>"

LineFeedReplace = Replace(Replace(Replace(Replace(Replace(Replace(str, Chr(10), strReplace), Chr(13), strReplace), vbCr , strReplace), vbCrLf, strReplace), vbLf, strReplace), vbNewLine, strReplace)

End Function

If cell A1 contains a string with a linefeed then =LineFeedReplace(A1) will return the string with all linefeeds set to <br>

Brycebryn answered 24/5, 2011 at 21:41 Comment(0)
S
3

First make sure you account for both CR and LF which tend to come together. The codes for these are 0013 and 0010 and so you will need a formula that allows you to clean both. I used this formula successfully =SUBSTITUTE(A3,CHAR(13),"<br>") to convert a cell of long text in excel replacing invisible breaks with the 'br' tag. Since you can't tell exactly what kind of line break you have you can also try it with 0010 or =SUBSTITUTE(A3,CHAR(10),"<br>")

Sturgis answered 3/12, 2014 at 19:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.