我的php代码如下:
if($ptp_car_1 == "ptp_Denpasar_Ubud_fee_".$way."_car_1"){
$ptp_car_1 = "ptp_Ubud_Denpasar_fee_".$way."_car_1";
}
if($ptp_car_1 == "ptp_Denpasar_Tanah_Lot_fee_".$way."_car_1"){
$ptp_car_1 = "ptp_Tanah_Lot_Denpasar_fee_".$way."_car_1";
}
if($ptp_car_1 == "ptp_Denpasar_Sanur_fee_".$way."_car_1"){
$ptp_car_1 = "ptp_Sanur_Denpasar_fee_".$way."_car_1";
}
if($ptp_car_1 == "ptp_Denpasar_Seminyak_fee_".$way."_car_1"){
$ptp_car_1 = "ptp_Seminyak_Denpasar_fee_".$way."_car_1";
}
if($ptp_car_1 == "ptp_Denpasar_Kuta_fee_".$way."_car_1"){
$ptp_car_1 = "ptp_Kuta_Denpasar_fee_".$way."_car_1";
}
if($ptp_car_1 == "ptp_Denpasar_Jimbaran_fee_".$way."_car_1"){
$ptp_car_1 = "ptp_Jimbaran_Denpasar_fee_".$way."_car_1";
}
if($ptp_car_1 == "ptp_Denpasar_Nusa_Dua_fee_".$way."_car_1"){
$ptp_car_1 = "ptp_Nusa_Dua_Denpasar_fee_".$way."_car_1";
}
// many code over 1,000
我该如何优化它? 我希望页面运行得更快,现在对它来说太慢了。
答案 0 :(得分:4)
只要其他1000多个字符串遵循相同的模式,此函数将直接转换字符串。
// reorder parts if the following pattern is true for all entries
//
// starts with "ptp" (store as group 1)
// then contains an '_'
// then has at least 1 char that is not an '_' (store as group 2)
// then contains an '_'
// then contains at least 1 char that is not '_fee_' (store as group 3)
// then contains an '_'
// then contains any number of chars (store as group 4)
//
// group 4 will contain 'fee_', $way and '_car_1' but we really don't need to care what
// those chars are, they just get tacked on to the end.
//
// Once the original is broken up, parts 2 & 3 are swapped and the
// results are imploded into the desired string with '_' separators.
//
function swapPtpParts($ptp_car_1) {
$matches = [];
if(preg_match("/(ptp)_([^_]+)_(.+?(?=_fee_))_(.*)/", $ptp_car_1, $matches)) {
$parts = [
$matches[1],
$matches[3],
$matches[2],
$matches[4],
];
return implode('_', $parts);
}
return $ptp_car_1;
}
用法:
$ptp_car_1 = swapPtpParts($ptp_car_1);
答案 1 :(得分:0)
如果如下,请使用其他:
if($ptp_car_1 == "ptp_Denpasar_Ubud_fee_".$way."_car_1"){
$ptp_car_1 = "ptp_Ubud_Denpasar_fee_".$way."_car_1";
}
elseif($ptp_car_1 == "ptp_Denpasar_Tanah_Lot_fee_".$way."_car_1"){
$ptp_car_1 = "ptp_Tanah_Lot_Denpasar_fee_".$way."_car_1";
}
elseif($ptp_car_1 == "ptp_Denpasar_Sanur_fee_".$way."_car_1"){
$ptp_car_1 = "ptp_Sanur_Denpasar_fee_".$way."_car_1";
}
elseif($ptp_car_1 == "ptp_Denpasar_Seminyak_fee_".$way."_car_1"){
$ptp_car_1 = "ptp_Seminyak_Denpasar_fee_".$way."_car_1";
}
elseif($ptp_car_1 == "ptp_Denpasar_Kuta_fee_".$way."_car_1"){
$ptp_car_1 = "ptp_Kuta_Denpasar_fee_".$way."_car_1";
}
elseif($ptp_car_1 == "ptp_Denpasar_Jimbaran_fee_".$way."_car_1"){
$ptp_car_1 = "ptp_Jimbaran_Denpasar_fee_".$way."_car_1";
}
elseif($ptp_car_1 == "ptp_Denpasar_Nusa_Dua_fee_".$way."_car_1"){
$ptp_car_1 = "ptp_Nusa_Dua_Denpasar_fee_".$way."_car_1";
}
会更快。
答案 2 :(得分:0)
在这种情况下,SWITCH CASE应该更好。
通过这个......
Why the switch statement and not if-else?
Is "else if" faster than "switch() case"?
尝试一下,
switch ($ptp_car_1){
case "ptp_Denpasar_Ubud_fee_".$way."_car_1":
{
$ptp_car_1 = "ptp_Ubud_Denpasar_fee_".$way."_car_1";
}
break;
case "ptp_Denpasar_Tanah_Lot_fee_".$way."_car_1":
{
$ptp_car_1 = "ptp_Tanah_Lot_Denpasar_fee_".$way."_car_1";
}
break;
case "ptp_Denpasar_Sanur_fee_".$way."_car_1":
{
$ptp_car_1 = "ptp_Sanur_Denpasar_fee_".$way."_car_1";
}
break;
case "ptp_Denpasar_Seminyak_fee_".$way."_car_1":
{
$ptp_car_1 = "ptp_Seminyak_Denpasar_fee_".$way."_car_1";
}
break;
case "ptp_Denpasar_Kuta_fee_".$way."_car_1":
{
$ptp_car_1 = "ptp_Kuta_Denpasar_fee_".$way."_car_1";
}
break;
case "ptp_Denpasar_Jimbaran_fee_".$way."_car_1":
{
$ptp_car_1 = "ptp_Jimbaran_Denpasar_fee_".$way."_car_1";
}
break;
case "ptp_Denpasar_Nusa_Dua_fee_".$way."_car_1":
{
$ptp_car_1 = "ptp_Nusa_Dua_Denpasar_fee_".$way."_car_1";
}
break;
/*
//OPTIONAL
default:
$ptp_car_1 = $ptp_car_1;
*/
}
答案 3 :(得分:0)
我还没有看到所有1000条if语句,但如果patten成立,你可以使用一个替换文本的函数。和以前一样:
function convertDenpansarFeeString($ptp_car, $way)
{
// number of characters in ptp_Denpasar_ = 13
// number of characters in _car_ = 5
return "ptp_"
. substr($ptp_car
, 13
, strpos($ptp_car, "_fee_") - 13
)
. "_Denpasar_fee_"
. $way
. "_car_"
. substr($ptp_car
, strpos($ptp_car, "_car_") + 5
);
}
并像这样使用:
$ptp_car1 = convertDenpansarFeeString($ptp_car1, $way);
$ptp_car2 = convertDenpansarFeeString($ptp_car2, $way);
答案 4 :(得分:0)
如何使用数组?
function getResult($key){
$way = 'hello';
$array = [
'ptp_Denpasar_Ubud_fee_'.$way.'_car_1' => "ptp_Ubud_Denpasar_fee_".$way."_car_1",
"ptp_Denpasar_Tanah_Lot_fee_".$way."_car_1" => "ptp_Sanur_Denpasar_fee_".$way."_car_1",
];
return $array[$key];
}
echo getResult('ptp_Denpasar_Ubud_fee_hello_car_1');