【发布时间】:2016-09-14 12:27:12
【问题描述】:
我正在为异常字符串使用 switch-function,应该为正确的 h1-titles 等进行更改。
我的目标是减少function ausnahme 的异常。
function ausnahme($value) {
global $value; // now the function ausnahme may change the variable
switch ($value) {
case "kern teilchenphysik": $value = "Kern-/Teilchenphysik";
break;
case "tipps tricks": $value = "Tipps & Tricks";
break;
case "erich honecker": $value = "Erich Honecker";
break;
case "ökogeographische regeln": $value = "Ökogeographische Regeln";
break;
}
return $value;
}
这个开关现在有 3000 行,我想要函数中这个开关之前的规则。
示例:要删除所有带有 2 个单词或更好的 x-words 的 switch-terms,它们都应该以大写字母开头我想要我的函数 bigwords:
例子:二字->二字,三字-表达->三字表达
有些词不能大写,例如'und'、'das';'der'、'die'等。
function bigwords($value) {
global $value;
$value = "X" . $value;
// Hilfsvariable,da & sonst auf Position 0 steht,
// was gleichbedeutend mit FALSE/NULL angesehen wird
if (strpos($value, "&a") == "1") {
$value = substr($value, 2);
$value = "&" . ucfirst($value);
} elseif (strpos($value, "&o") == "1") {
$value = substr($value, 2);
$value = "&" . ucfirst($value);
} elseif (strpos($value, "&u") == "1") {
$value = substr($value, 2);
$value = "&" . ucfirst($value);
} else {
// X wieder entfernen
$value = substr($value, 1);
$value = ucfirst($value);
}
}
我可以在切换之前使用这部分删除许多切换行,因为对于其他一些情况不同或期望出现异常,我会称之为;-)。 另一个问题是德语变音符号:
示例:“oeffnung-boerse-oekologie”现在是:“Öffnung börse ökologie”
我想用function bigwords 和function umlaute 来实现:'Öffnung Börse Ökologie'。
function umlaute($value) {
global $value; // 12.07.14 str_replace changed from eregi_replace/ 22.07.14 mehrere str_replace mit arrays geändert
$from = ['-', 'ae', 'oe', 'ue', 'Ae', 'Oe', 'Ue'];
$to = [' ', 'ä', 'ö', 'ü', 'Ä', 'Ö', 'Ü'];
$value = mb_strtolower(str_replace($from, $to, $value), 'utf-8');
// ACHTUNG! Dadurch greift die "erster Buchstabe Groß"-Regel nicht mehr, da erster
// "Buchstabe" nun das Kaufmanns-Und ist! -> wird in bigwords ersetzt
}
// 最后 2 个 cmets 可能已过时,因为我认为 bigwords 规则适用于此功能。我以前的程序员在这个函数之后编写脚本的第一个字母是“&”而不是字母。
对于现有的function bigwords 和function umlaute,请随时提供我的提示
谢谢你的帮助。
【问题讨论】:
-
您能否为您的开关提供一个示例输入,并告诉我们您想用您的功能实现什么。因为我不明白你的用语:“删除许多开关线”、“x-words”。
-
那么为什么全局
$value不只是设置为您首先需要的?您正在以任何方式设置它。看看整体架构,尽管您可能想要创建一个可以为您保存所需页面元素的页面类。 -
请不要将变量传递给函数然后访问它们
global。那没有意义。只需传递变量by reference -
会建议在参考上传递 $value,如 &$value。这应该意味着您可以摆脱全局。
-
@simon 我知道
global不应该被使用,但我不知道没有它不同的功能应该如何工作。在我的大 function_inc.php 文件中,function ausnahme和更多我发现 8times global :-(.
标签: php function switch-statement