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

时间:2021-11-30 07:25:08

I got this code

我有这个代码

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

which shows the values Title and Description inside a box, but it does so directly following each other. When the box is too small it does a line break, but only at the exact point of the end of the box. so how can i force a line break between $reservation->Title and $reservation->Description ?

它显示了框内的值Title和Description,但它直接相互跟随。当盒子太小时,它会换行,但只能在盒子末端的确切位置。那我怎么能强制$ reservation-> Title和$ reservation-> Description之间换行?

Here is the Full Name Class

这是全名类

        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 个解决方案

#1


0  

Not a good way without proper explanation, but a Quick solution

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

Replace

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

with

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

#2


0  

HTML line break can be inserted as:

HTML换行符可以插入为:

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

or with generic (non-HTML) new-line character:

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

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

It is important to use double quotes in last case (").

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

#3


0  

See the link for working example: http://codepad.viper-7.com/qS7nNv

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

You can add a third parameter to the class.

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

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;
}
 }

then add the delimiter:

然后添加分隔符:

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

#1


0  

Not a good way without proper explanation, but a Quick solution

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

Replace

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

with

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

#2


0  

HTML line break can be inserted as:

HTML换行符可以插入为:

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

or with generic (non-HTML) new-line character:

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

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

It is important to use double quotes in last case (").

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

#3


0  

See the link for working example: http://codepad.viper-7.com/qS7nNv

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

You can add a third parameter to the class.

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

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;
}
 }

then add the delimiter:

然后添加分隔符:

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