Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Stripping line-end stuff from a html template

Status
Not open for further replies.

awingnut

Programmer
Feb 24, 2003
759
US
I have an HTML template that I read as s single string using fread(). My problem is that when I output the template (in an email attachment) there are all kinds of spurious line breaks which apparently are teh result of line-end stuff in the string. I tried using implode/explode to get rid of '\r' and '\n' but there seems to be something else that causes these breaks. Can someone tell me what characters I need to be stripping out to get this string corrected. TIA.
 
1. HTML does not interpret whitespace. \r or \n only will show in the HTML source code.
2. implode/explode doesn't seem to be the best mechanism to remove \r or \n

Use str_replace():
Code:
$string = str_replace(array("\r","\n"),'',$string);

This will remove all instances of the \r and \n characters. However, I don't understand how the breaks manifest themselves. Where do they show? What makes you say they are spurious?
 
Good question. After more examination it appears to be more sinister then that. It seems splits are in random locations. For example:

Template:
Code:
<html>
<head>
<style type="text/css">
<!--
body,p,div,span,td,input,textarea {
    font-family: "Times New Roman", Times, serif; font-size: 10pt;
}
h4 {
   page-break-before: always;
}
-->
</style>
Resulting Raw Source:
Code:
<html><head><styl
e type="text/css"><!--body,p,div,span,td,input,textarea {	font-family: "Time
s New Roman", Times, serif;	font-size: 10pt;}h4 {	page-break-before: always;
}--></style>

While this in and of itself does not really effect the display it is an easy example. There are other places where this occurs that does effect the display (although I too cannot see why it matters). However, this is designed to be an attachment to an email and is intended to be printer friendly. I don't know if the Transfer-Encoding (quoted-printable) causes it to behave differently but I am pretty sure if I can get a contigous single string after the read, it will work (I use split_chunk() to add the post-processed string to the email body).
 
What mechanism of e-mail generation do you use?
PHPMailer class phpmailer.sourceforge.net handles attachments beautifully - just a recommendation. May save you a lot of headache.
 
I'm just using PHP send(). It works fine for other stuff. I need to look closer at the code that processes that string but I think the issus is still how it looks after fread().
 
How are you using fread()? Local file system? Any kind of wrappers enabled?
That might give a clue.
 
Code:
$fid=fopen("<local file path>","r");
if ($fid) {
   $str=$fread($fid,filesize("<local file path>"));
   $str=str_replace(array("\n","\r"),'',$str);
   .
   .
   .
 
Have you tried file_get_contents()?

So, what you are saying is that right when reading the file in it is already chunked? Compare the output of fread() with file_get_contents.
 
Thanks for all the help but I found it. It turned out to be split_chunk() that was messing it up (don't know why). I also don't know why I thought I needed to use it but eliminating it cured the problem.
 
I have never seen any function split_chunk() in the PHP manual. Generic? Self developed?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top