Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/244.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/77.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP:如何防止不必要的换行_Php_Html_Xhtml - Fatal编程技术网

PHP:如何防止不必要的换行

PHP:如何防止不必要的换行,php,html,xhtml,Php,Html,Xhtml,我正在使用PHP创建一些基本的HTML。标记始终相同,但实际的链接/标题对应于PHP变量: $string = '<p style="..."><a href="'.$html[$i].'"><strong><i>'.$title[$i].'</i></strong></a> <br>'; echo $string; fwrite($outfile, $string); $string=' ",;

我正在使用PHP创建一些基本的HTML。标记始终相同,但实际的链接/标题对应于PHP变量:

$string = '<p style="..."><a href="'.$html[$i].'"><strong><i>'.$title[$i].'</i></strong></a>
<br>';
echo $string;
fwrite($outfile, $string);
$string='


",; echo$字符串; fwrite($outfile,$string);

生成的html(当我查看页面源代码时)和我正在写入的简单txt文件中的内容如下:

    <p style="..."><a href="http://www.example.com
"><strong><i>Example Title
</i></strong></a></p>
<br>



虽然这样做有效,但这并不是我想要的。每次我中断字符串以插入变量时,PHP都会添加一个换行符。有什么方法可以防止这种行为吗?

虽然换行符根本不会影响HTML页面(除非您使用的是
pre
text wrap:pre
),但您应该能够对这些变量调用
trim()
,以删除换行符

要了解变量的前面或后面是否有换行符,请尝试以下正则表达式

var_dump(preg_match('/^\n|\n$/', $variable));

(我认为您必须使用单引号,以便PHP不会将您的
\n
转换为字符串中的文字换行)。

我猜应该怪您的变量。您可以尝试使用
trim
:::。

断行显示是因为多字节编码,我相信。尝试:

$newstring =  mb_substr($string_w_line_break,[start],[length],'UTF-8');

当解析html后出现奇怪的换行符时,这对我很有效。

您是否绝对确定插入的变量末尾没有换行符?试着用trim()将它们包裹起来,看看是否会停止。如果是这样,那么你的变量实际上包含换行符。谢谢,我使用了
rtrim
,解决了这个问题。既然你先解决了这个问题,如果你在24小时内加上一个答案,我一定会接受的。