I've inherited a MySQL DB that contains email subjects and message bodies generated in Microsoft Outlook (both Windows and Mac OS X versions).
When I attempt to use PHP to pull the message bodies from the DB and then run json_encode
on them, the JSON has a number of (non-escaped) newlines within it, thus invalidating the output.
I had never seen this before, but upon further investigation, I realized that every instance of a newline was between a <style>
tag and a </style>
tag that (seemingly) had nothing between them other than a newline.
I then ran preg_match
to capture the whitespace/newlines between one set of style
tags. Upon doing that and running strlen
on the results, I got back the number 2460
.
I then looped through each character in the captured "whitespace" and attempted to output them. At first, I only saw whitespace, but when I added "\n"
between each character, then I suddenly saw the following output (minus some potential leading and trailing whitespace):
<
!
-
-
/
*
F
o
n
t
D
e
f
i
n
i
t
i
o
n
s
*
/
@
f
o
n
t
-
f
a
c
e
{
f
o
n
t
-
f
a
m
i
l
y
:
H
e
l
v
e
t
i
c
a
;
p
a
n
o
s
e
-
1
:
2
1
1
6
4
2
2
2
2
2
4
;
}
@
f
o
n
t
-
f
a
c
e
{
f
o
n
t
-
f
a
m
i
l
y
:
H
e
l
v
e
t
i
c
a
;
p
a
n
o
s
e
-
1
:
2
1
1
6
4
2
2
2
2
2
4
;
}
@
f
o
n
t
-
f
a
c
e
{
f
o
n
t
-
f
a
m
i
l
y
:
C
a
l
i
b
r
i
;
p
a
n
o
s
e
-
1
:
2
1
5
5
2
2
2
4
3
2
4
;
}
@
f
o
n
t
-
f
a
c
e
{
f
o
n
t
-
f
a
m
i
l
y
:
T
a
h
o
m
a
;
p
a
n
o
s
e
-
1
:
2
1
1
6
4
3
5
4
4
2
4
;
}
@
f
o
n
t
-
f
a
c
e
{
f
o
n
t
-
f
a
m
i
l
y
:
C
o
n
s
o
l
a
s
;
p
a
n
o
s
e
-
1
:
2
1
1
6
9
2
2
4
3
2
4
;
}
/
*
S
t
y
l
e
D
e
f
i
n
i
t
i
o
n
s
*
/
p
.
M
s
o
N
o
r
m
a
l
,
l
i
.
M
s
o
N
o
r
m
a
l
,
d
i
v
.
M
s
o
N
o
r
m
a
l
{
m
a
r
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-
t
y
p
e
:
p
e
r
s
o
n
a
l
-
r
e
p
l
y
;
f
o
n
t
-
f
a
m
i
l
y
:
"
C
a
l
i
b
r
i
"
,
"
s
a
n
s
-
s
e
r
i
f
"
;
c
o
l
o
r
:
#
1
F
4
9
7
D
;
}
.
M
s
o
C
h
p
D
e
f
a
u
l
t
{
m
s
o
-
s
t
y
l
e
-
t
y
p
e
:
e
x
p
o
r
t
-
o
n
l
y
;
f
o
n
t
-
s
i
z
e
:
1
0
.
0
p
t
;
}
@
p
a
g
e
W
o
r
d
S
e
c
t
i
o
n
1
{
s
i
z
e
:
8
.
5
i
n
1
1
.
0
i
n
;
m
a
r
g
i
n
:
1
.
0
i
n
1
.
0
i
n
1
.
0
i
n
1
.
0
i
n
;
}
d
i
v
.
W
o
r
d
S
e
c
t
i
o
n
1
{
p
a
g
e
:
W
o
r
d
S
e
c
t
i
o
n
1
;
}
-
-
>
Here is the output without newlines:
<!--/* Font Definitions */@font-face {font-family:Helvetica; panose-1:2 11 6 4 2 2 2 2 2 4;}@font-face {font-family:Helvetica; panose-1:2 11 6 4 2 2 2 2 2 4;}@font-face {font-family:Calibri; panose-1:2 15 5 2 2 2 4 3 2 4;}@font-face {font-family:Tahoma; panose-1:2 11 6 4 3 5 4 4 2 4;}@font-face {font-family:Consolas; panose-1:2 11 6 9 2 2 4 3 2 4;}/* Style Definitions */p.MsoNormal, li.MsoNormal, div.MsoNormal {margin:0in; margin-bottom:.0001pt; font-size:12.0pt; font-family:"Times New Roman","serif";}a:link, span.MsoHyperlink {mso-style-priority:99; color:blue; text-decoration:underline;}a:visited, span.MsoHyperlinkFollowed {mso-style-priority:99; color:purple; text-decoration:underline;}p.MsoPlainText, li.MsoPlainText, div.MsoPlainText {mso-style-priority:99; mso-style-link:"Plain Text Char"; margin:0in; margin-bottom:.0001pt; font-size:10.5pt; font-family:Consolas;}p {mso-style-priority:99; margin:0in; margin-bottom:.0001pt; font-size:12.0pt; font-family:"Times New Roman","serif";}p.MsoAcetate, li.MsoAcetate, div.MsoAcetate {mso-style-priority:99; mso-style-link:"Balloon Text Char"; margin:0in; margin-bottom:.0001pt; font-size:8.0pt; font-family:"Tahoma","sans-serif";}span.PlainTextChar {mso-style-name:"Plain Text Char"; mso-style-priority:99; mso-style-link:"Plain Text"; font-family:Consolas;}span.BalloonTextChar {mso-style-name:"Balloon Text Char"; mso-style-priority:99; mso-style-link:"Balloon Text"; font-family:"Tahoma","sans-serif";}p.msochpdefault, li.msochpdefault, div.msochpdefault {mso-style-name:msochpdefault; margin:0in; margin-bottom:.0001pt; font-size:10.0pt; font-family:"Times New Roman","serif";}span.plaintextchar0 {mso-style-name:plaintextchar; font-family:Consolas;}span.balloontextchar0 {mso-style-name:balloontextchar; font-family:"Tahoma","sans-serif";}span.emailstyle22 {mso-style-name:emailstyle22; font-family:"Calibri","sans-serif"; color:black;}span.emailstyle23 {mso-style-name:emailstyle23; font-family:"Calibri","sans-serif"; color:#1F497D;}span.EmailStyle27 {mso-style-type:personal-reply; font-family:"Calibri","sans-serif"; color:#1F497D;}.MsoChpDefault {mso-style-type:export-only; font-size:10.0pt;}@page WordSection1 {size:8.5in 11.0in; margin:1.0in 1.0in 1.0in 1.0in;}div.WordSection1 {page:WordSection1;}-->
After doing some research, this appears to be some styling markup created by Microsoft Office products (including Outlook). What baffles me though is why I can only see these characters when I loop through each character in the string and output them with newline characters between them.
What further baffles me is how to detect these characters and get rid of them so that the JSON output is correctly formatted without the newlines.
I tried running things like preg_replace('!\s!', $email_msg_body)
on each of the email message bodies, but for some reason, the newlines between the style
tags are still there.
Ultimately, I'm just looking for a way to properly format the JSON string so that I can output it. More than that though, I would like to understand why this is happening in the first place and how to prevent it in the future.
Sadly, because I inherited this DB from someone else, I have no clue how the feed that moves the Outlook emails to the DB was set up in the first place.
The DB is also latin1
encoded. At first, I thought that the hidden string of characters might contain a non-valid ASCII character, but after running ord
on each character in the string, that doesn't seem to be the case.
Any help would be much appreciated.
Thank you.
json_encode()
would generate invalid json. Can you post an example on for example codepad.viper-7.com?style
tags) are properly escaped and output.