我如何爆炸()这些变量?

时间:2016-09-08 02:35:40

标签: php

我有3个变量。

$Full Name = 'John Smith'
$Address = '4732 E Cherry St'
$Type = array('Residential','Commercial');

然后我需要检查$Type给我$JobName的结尾,这样我就会做以下事情......

if ($type == 'Residential') { $JobType = 'R'; }
else if ($type == 'Commercial') { $JobType = 'C'; }

我需要获得$JobName = 'Smith-4732-R'

的输出

那么我如何从$FullName var中删除姓氏,只从$Address中删除地址#给我 Smith-4732-R

修订代码......

$FullName = 'John Smith';
$Address = '4732 E Cherry St';
$Type = 'Residential';

$parts = explode(" ", $FullName);
$ClientLastName = array_pop($parts);

$parts = explode(" ", $Address);
$AddressSnip = print_r(array_slice($parts,0,1));

if ($Type == 'Residential') { $JobType = 'R'; }
else if ($Type == 'Commercial') { $JobType = 'C'; }


echo $ClientLastName . '-' . $AddressSnip  . '-' . $JobType;

当我执行上述操作时,我得到以下内容。 Array ( [0] = 4732 ) Smith-1-R

2 个答案:

答案 0 :(得分:1)

尝试:

$FullName = explode(" ",$FullName);
$lastName = $FullName[sizeof($FullName)-1];

$Address = explode(" ",$Address);
$Address = $Address[0];

echo $lastName . '-' . $Address  . '-' . $JobType;

应该有效。希望它有帮助=)

答案 1 :(得分:0)

使用the explode() function。 E.g:

$JobName = end(explode(" ",$FullName)).'-'.explode(" ",$Address)[0].'-'.$JobType;