简体   繁体   English

PHP包含源代码中不需要的换行符

[英]Unwanted line break in source code with php include

I am using this script to generate a regular-changing email address with qmail 我正在使用此脚本通过qmail生成定期更改的电子邮件地址

#!/bin/bash

##### change these settings #####

qmailpath="/your/path/to/.qmail-"
wwwpath="/your/path/to/html/tempmail"
host="@my.host"
prefix="temp-"
length="16"

##### do not change the following #####

oldmail="$qmailpath"$(cat "$wwwpath" | cut -d@ -f1)
newmail="$prefix"$(date +%s%N | md5sum | cut -c 1-"$length")

rm "$oldmail"
touch "$qmailpath$newmail"
echo -n "$newmail$host" > "$wwwpath"

I am using this php command to include the email address in my webpage: 我正在使用此php命令在我的网页中包含电子邮件地址:

<a href="mailto:<?php include('tempmail');?>"><?php include('tempmail');?></a>

It works fine, but if I take a look at the source code of the webpage, I recognize a strange line break: 它工作正常,但是如果我看一下网页的源代码,就会发现一个奇怪的换行符:

<a href="mailto:temp-rpwaa44hff5kch8smfuv3@myhost
">temp-rpwaa44hff5kch8smfuv3@myhost
</a>

Where does this come from and how can I remove it? 它来自哪里,如何删除?

UPDATE : I updated the script and added -n after echo . 更新 :我更新了脚本,并在echo之后添加了-n Now it works. 现在可以了。

It appears there is an end of line, to solve this in PHP use rtrim(file_get_contents()) like so: 似乎有一行结尾,要在PHP中解决此问题,请使用rtrim(file_get_contents())如下所示:

<a href="mailto:<?php echo rtrim(file_get_contents('tempmail'));?>"><?php echo rtrim(file_get_contents('tempmail'));?></a>

From the php manual: 从php手册:

file_get_contents() is the preferred way to read the contents of a file into a string. file_get_contents()是将文件内容读取为字符串的首选方法。 It will use memory mapping techniques if supported by your OS to enhance performance. 如果操作系统支持,它将使用内存映射技术来提高性能。

On the bash side of things, it is echo that introduces a newline, so if you don't want it just use echo -n : 在bash方面,echo引入了换行符,因此,如果您不想要它,只需使用echo -n

echo -n $TEMPMAIL"@myhost" > /home/user/html/tempmail

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM