问题描述
我正在构建一个处理URL的脚本,并会根据各种条件对其进行修改。
I am building a script that will process a URL, and will modify it according to various conditions.
我不确定最佳做法是什么这个;例如,现在,在合作伙伴A部分下,如果类别与premiuim不同,我的 final_url
变量将保持为空。我想我可以创建一个条件,检查它是否为空,在这种情况下它应该适用于它 $ url3
的值,但它感觉不够聪明:
I'm not sure what is the best practice to do this; for example, right now, under "Partner A" section, if the category is different than "premiuim", my final_url
variable will remain empty. I guess I can make a condition that will check if it's empty and in that case it should apply to it the value of $url3
, but it doesn't feel smart enough:
<?php
$url = "http://www.default.com"; // default URL
$url = "something"; // this part will come from a database
$id="something"; // some value to insert later into URL
// the following rules will be followed according to various parameters not defined in this sample code
if($partner == "A") { // Partner A rule:
$url2 = str_replace('777', '999', $url); // Inital Change
$url3 = str_replace('?location', '?id=' . $id . '&location', $url2); // Add id, anytime ?location shows up
if($category == "premium") { // premium category rules:
$re = "/(?<=TID%3D)\\d+/"; // String to Replace in URL (digits)
$newtid = "4000"; // Default New TID
if($special == "yes") { $newtid = "8000";} // special TID value
$final_url = preg_replace($re, $newtid, $url3); // replace the TID
}
};
if($partner == "B") { // Partner B rule:
$final_url = str_replace('status=1', 'config=' . $id . '&status=1', $url); //Add id, anytime status=1 exists in url
};
?>
<p>Final URL: <?php echo $final_url; ?></p>
此外,我不确定整个结构是否是最佳结构。
Also, I'm not sure that the whole structure is the optimal one, in general.
编辑:
更新代码:
if($category == 'A') {
// A category rules:
$url2 = str_replace('aaa', 'bbb', $url); // Global rule #1
$url3 = str_replace('?url', '?id=' . $id . '&url', $url2); // Glbal rule #2
$final_url = $url3; // Final URL unless following cases occur
if($offer == 'Special') {
//category is A and offer is special
$re = "/(?<=TID%3D)\\d+/"; // replace rule for current case
$tid = "123"; // Default New TID value for current case
$final_url = preg_replace($re, $tid, $url3); // Final URL for this case unless following case occur
if ($discount == '10') {
// category is A, offer is special, and Discount is 10
$re = "/(?<=TID%3D)\\d+/"; // replace rule for current case
$tid = "999"; // Special TID value for current case
$final_url = preg_replace($re, $itd, $url3); // Final URL for this case
}
}
}
推荐答案
这应该简化代码:
if($category == 'A') {
// A category rules:
$final_url = str_replace(array('aaa', '?url'), array('bbb', '?id=' . $id . '&url'), $url);
$re = "/(?<=TID%3D)\\d+/"; // replace rule for current case
if($offer == 'Special') {
//category is A and offer is special
$tid = "123"; // Default New TID value for current case
if ($discount == '10') {
// category is A, offer is special, and Discount is 10
$tid = "999"; // Special TID value for current case
}
$final_url = preg_replace($re, $tid, $url3);
}
}
你的正则表达式是一样的,所以你不需要定义它两次。 str_replace
也可以使用多个搜索字段并替换值。
Your regex is the same so you don't need to define it twice. The str_replace
can also take multiple search fields and replaces values.
这篇关于如何将各种IF条件合并为最终结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!