删除 php 中的换行符

removing line breaks in php

如标题所示,我正在使用 echo 函数创建一个 h3 字符串,它将插入一个 php 值 $lowprice 和 $highprice。目标是阅读文本

这里是所有价格在 $lowprice 和 $highprice 之间的房子。代码像这样分成几行

这里是价格在$

之间的所有房子

100000 和$

500000 :

这是我写的代码,如何把它全部放在一行上。

<?php
echo '<caption>';
echo '<h3> Here are all the houses whose prices are between $ </h3>'.$lowprice.'<h3> and $</h3>'.$highprice.'<h3> : </h3>';
echo '</caption>';
?>

<h3> 是一个块元素,意味着它将占据一整行。我认为您想用内联元素 <span> 标记替换内部 <h3>

像这样:

<?php
  echo '<caption>';
  echo '<h3> Here are all the houses whose prices are between $ <span>'.$lowprice.'</span> 
  and $<span>'.$highprice.'</span></h3>';
  echo '</caption>';
?>

或者您可以简单地一起删除所有内部标签,如下所示:

<?php
  echo '<caption>';
  echo '<h3> Here are all the houses whose prices are between $'.$lowprice.' and $'.$highprice.'</h3>';
  echo '</caption>';
?>

出现换行符是因为您制作了多个 h3 元素。您在每次插入时关闭并重新打开 h3 标签,这是没有必要的。您的代码的 html 输出如下:

<h3>Here are all the houses whose prices are between $</h3>
<h3>100000 and $</h3>
<h3>500000 : </h3>

自动添加分隔符,因为这是 h3 元素的行为。

你需要的是:

echo '<h3> Here are all the houses whose prices are between $'.$lowprice.' and $'.$highprice.':</h3>';

更好的是,不要使用 echo 来定义您的 html; html 和 php 在同一个文件中可以互换。一个更清晰、更易读且更易于维护的解决方案是像这样形成你的脚本:

<caption>
    <h3>Here are all the houses whose prices are between $<?= $lowprice ?> and $<?= $highprice ?>:</h3>
</caption>

通常,您可以像这样在 php 和 html 之间切换:

<?php
...do some php
?>
<somehtml></somehtml>
<?php do some more php ?>
<morehtml>...

请注意 <?=<?php echo 的 shorthand。