如何在php中的两个值中添加一个中断?

时间:2013-01-28 13:41:33

标签: php html mysql tags

我收到了这段代码

       $title = new FullName($reservation->Title, $reservation->Description)

显示框内的标题和描述值,但它直接相互跟随。当盒子太小时它会断线,但只在盒子末端的确切位置。那么如何强制在$ reservation-> Title和$ reservation-> Description?

之间换行

这是全名类

        class FullName
        {
/**
 * @var string
 */
private $fullName;

public function __construct($firstName, $lastName)
{
    $formatter = Configuration::Instance()->GetKey(ConfigKeys::NAME_FORMAT);
    if (empty($formatter))
    {
        $this->fullName = "$firstName $lastName";
    }
    else
    {
        $this->fullName = str_replace('{first}', $firstName, $formatter);
        $this->fullName = str_replace('{last}', $lastName, $this->fullName);
    }
}

public function __toString()
{
    return $this->fullName;
}

}

3 个答案:

答案 0 :(得分:0)

如果没有正确的解释,这不是一个好方法,而是快速解决方案

替换

$title = new FullName($reservation->Title, $reservation->Description)

   $t = $reservation->Title . "<br />";
   $d = $reservation->Description;
   $title = new FullName($t, $d);

答案 1 :(得分:0)

HTML换行符可以插入:

$this->fullName = $firstName . '<br />' . $lastName

或使用通用(非HTML)换行符:

$this->fullName = $firstName . "\n" . $lastName

在最后一种情况下使用双引号很重要(“)。

答案 2 :(得分:0)

请参阅工作示例的链接:http://codepad.viper-7.com/qS7nNv

您可以向该类添加第三个参数。

class FullName
{
/**
 * @var string
 */
private $fullName;

public function __construct($firstName, $lastName, $delimiter = null)
{
    $formatter = Configuration::Instance()->GetKey(ConfigKeys::NAME_FORMAT);
    if (empty($formatter))
    {
      if($delimiter) {
        $this->fullName = "$firstName $delimiter $lastName";
      } else {
        $this->fullName = "$firstName $lastName";
      }
    }
    else
    {
        $this->fullName = str_replace('{first}', $firstName, $formatter);
        $this->fullName = str_replace('{last}', $lastName, $this->fullName);
    }
}

public function __toString()
{
    return $this->fullName;
}
 }

然后添加分隔符:

$title = new FullName($reservation->Title, $reservation->Description, "<br />");