如何连接 php 中的多个字符串?
How to concatenate multiple strings in php?
我想连接 3 个字符串。对于第一个字符串,我将其拆分以获得前 4 个字母。另一个是 user_name
,最后一个是 date_of_application
。
我们在 PHP 中使用 .
(点运算符)。但正如我在这方面所做的那样,结果前两个字符串连接得很好,但第三个字符串即 date_of_application
,只有前 2 个或 4 个字母被连接而不是整个字符串。
$this-> member_id = mb_substr($this->country, 0, 4) . $this->user_name . $this->date_of_application;
输入:
28/08/2016,28082016
结果:
Indisid128/0, Indisiddhi28
编辑:
我想从 date.Input 日期中获取数字,可以是任何格式。例如-如果输入日期是 2016 年 8 月 28 日。输出应该是连接字符串 _ 28082016。我怎样才能得到这个?
怎么了?
您不能将 date
与 string
与现有格式连接起来。您需要对其进行转换(删除斜线)。
写date('Y_m_d-H-i-s', strtotime($this->date_of_application));
编写如下代码:
$country = mb_substr($this->country, 0, 4);
$username = $this->user_name;
$converted_date = date('Y_m_d-H-i-s', strtotime($this->date_of_application));
$this->member_id = $country . $username . $converted_date;
$date_of_application = date('m/d/Y');
$user_name = 'TestUser';
$country = 'India';
echo $member_id = mb_substr($country, 0, 4) . $user_name . $date_of_application;
我试了一下,效果很好
考虑将 $this->date_of_application 的值转换为 string
,例如:
$this->member_id = mb_substr($this->country, 0, 4) . $this->user_name . (String)$this->date_of_application;
或者,在您希望在字符串中使用变量值的情况下,您可以使用插值而不是连接,例如:
$this->member_id = mb_substr($this->country, 0, 4) . "{$this->user_name}{$this->date_of_application}";
我想连接 3 个字符串。对于第一个字符串,我将其拆分以获得前 4 个字母。另一个是 user_name
,最后一个是 date_of_application
。
我们在 PHP 中使用 .
(点运算符)。但正如我在这方面所做的那样,结果前两个字符串连接得很好,但第三个字符串即 date_of_application
,只有前 2 个或 4 个字母被连接而不是整个字符串。
$this-> member_id = mb_substr($this->country, 0, 4) . $this->user_name . $this->date_of_application;
输入:
28/08/2016,28082016
结果:
Indisid128/0, Indisiddhi28
编辑:
我想从 date.Input 日期中获取数字,可以是任何格式。例如-如果输入日期是 2016 年 8 月 28 日。输出应该是连接字符串 _ 28082016。我怎样才能得到这个?
怎么了?
您不能将 date
与 string
与现有格式连接起来。您需要对其进行转换(删除斜线)。
写date('Y_m_d-H-i-s', strtotime($this->date_of_application));
编写如下代码:
$country = mb_substr($this->country, 0, 4);
$username = $this->user_name;
$converted_date = date('Y_m_d-H-i-s', strtotime($this->date_of_application));
$this->member_id = $country . $username . $converted_date;
$date_of_application = date('m/d/Y');
$user_name = 'TestUser';
$country = 'India';
echo $member_id = mb_substr($country, 0, 4) . $user_name . $date_of_application;
我试了一下,效果很好
考虑将 $this->date_of_application 的值转换为 string
,例如:
$this->member_id = mb_substr($this->country, 0, 4) . $this->user_name . (String)$this->date_of_application;
或者,在您希望在字符串中使用变量值的情况下,您可以使用插值而不是连接,例如:
$this->member_id = mb_substr($this->country, 0, 4) . "{$this->user_name}{$this->date_of_application}";