0

我得到了这个代码

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

它在一个框中显示值 Title 和 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;
}

}

4

3 回答 3

0

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

代替

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

   $t = $reservation->Title . "<br />";
   $d = $reservation->Description;
   $title = new FullName($t, $d);
于 2013-01-28T13:46:49.557 回答
0

HTML换行符可以插入为:

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

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

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

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

于 2013-01-28T13:46:49.913 回答
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 />");
于 2013-01-28T13:59:40.930 回答