改进PHP函数

改进PHP函数,php,function,switch-statement,Php,Function,Switch Statement,我对异常字符串使用了一个开关函数,应该为正确的h1标题等进行更改 我的目标是减少函数ausnahme的异常 function ausnahme($value) { global $value; // now the function ausnahme may change the variable switch ($value) { case "kern teilchenphysik": $value = "Kern-/Teilchenphysik";

我对异常字符串使用了一个开关函数,应该为正确的h1标题等进行更改

我的目标是减少
函数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字删除所有开关术语,这些单词都应该以大写字母开头,我希望我的函数bigwords:

示例:两个词->两个词,三个词表达->三个词表达

有些词不允许大写,如“und”、“das”"死亡","死亡"等

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);
    }
} 
我可以在切换之前删除许多具有此部分的切换线路,因为对于其他一些情况,它们是不同的,或者期望出现异常,我将其称为;-)。 另一个问题是德国umlauts:

例如:“oeffnung boerse oekologie”现在是:“Öffnung börseökologie” 我想用
函数bigwords
函数umlaute
来实现:“Öffung 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 = [' ', 'ä', 'ö', 'ü', 'Ä', 'Ö', '&Uuml'];
    $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
}
//最后两条评论可能已经过时了,因为我认为bigwords规则与这个函数配合使用。我以前的程序员写脚本,在这个函数之后,第一个字母是“&”,不再是字母了

请随时为现有的
函数bigwords
函数umlaute

谢谢你的帮助

如果我能很好地理解你的话,这应该可以做到:

<?php 

$noUpper = [];
foreach (
    ['und','das','der','die']
    as $k
) $noUpper[$k] = true;
function prettify($inStr){
    global $noUpper;
    $out = [];
    foreach (
        explode(" ", $inStr)
        as $chunk
    ) array_push($out, @ $noUpper[$chunk] ? $chunk : ucfirst($chunk));
    return htmlentities(implode(" ", $out));
};

echo prettify("hello und me no das & der die"); // Sorry: I don't understand German ;-)

// Output: "Hello und Me No das &amp; der die"

如果我能很好地理解你的话,这应该可以做到:

<?php 

$noUpper = [];
foreach (
    ['und','das','der','die']
    as $k
) $noUpper[$k] = true;
function prettify($inStr){
    global $noUpper;
    $out = [];
    foreach (
        explode(" ", $inStr)
        as $chunk
    ) array_push($out, @ $noUpper[$chunk] ? $chunk : ucfirst($chunk));
    return htmlentities(implode(" ", $out));
};

echo prettify("hello und me no das & der die"); // Sorry: I don't understand German ;-)

// Output: "Hello und Me No das &amp; der die"


您能否为您的交换机提供一个示例输入,并告诉我们您想用您的功能实现什么。因为我不理解你的术语:“删除许多开关线路”、“x字”。那么为什么全局
$value
不只是设置为您首先需要的值呢?您正在以任何方式设置它。尽管您可能希望创建一个页面类来保存所需的页面元素,但请查看总体架构。请不要将变量传递给函数,然后访问它们
全局
。那没有道理。只需传递变量,就可以在引用上传递$value,如&$value。这意味着您可以去掉全局函数。@simon我知道不应该使用全局函数,但我不知道没有它不同的函数应该如何工作。在我的大function_inc.php文件中,我发现了8times global:-(.你能为你的交换机提供一个示例输入,并告诉我们你想用你的函数实现什么。因为我不理解你的术语:“删除许多开关行”,“x字”。那么,为什么全局
$value
不只是设置为您首先需要的值呢?您正在以任何方式设置它。尽管您可能希望创建一个可以保存所需页面元素的页面类,但请查看总体架构。请不要将变量传递给函数,然后访问它们
全局
。这样做了没有意义。只传递变量会建议像&$value一样在引用上传递$value。这应该意味着你可以去掉全局。@simon我知道不应该使用
global
,但我不知道没有它不同的函数应该如何工作。在我的大function_inc.php文件中,其中
function ausnahme
和许多我还发现了8倍全球:-(.我认为这可能是一个有效的解决方案。2个问题:这是使用
全局
的好方法吗?在
$value
中将
$inStr
重命名在哪里?您的示例与我的示例不同。每个单词都用“-”分隔,就像您的示例“hello und me das-&-der die”。请添加示例输出,如echo prettify(“oeffnung boerse und oekologie”);应该有输出:“Öffnung Böerse undÖkologie。也许函数需要“UTF-8”的地方。Q1:肯定不是!!如果可以选择,即使使用php本身也不是好主意:-P.我只是时间不多,不得不在效率和优雅之间做出选择;-)。或者你可以使用
['und'=>true,'das'=>true,…]
解决方案在函数内部,或者简单地将其构建物foreach移动到内部(但这意味着每次调用时都要重新构建它)。正确的解决方案是使用闭包。最新的PHP版本理论上都带有闭包,但我看到的东西看起来一团糟(我不确定它是否有用……。。。。。。。(在PHP中)最后,更好的解决方案是依赖(凌乱的)静态变量+if station(我将打断我的答案;-)。问题2:正如我所说,我写得很快,我无意中忽略了一个事实,即你用“-”而不是空格(“-”)分隔。但修复起来很简单,只需将
explode(…)
语句中的粘合字符串替换掉即可。注意,也可以通过对每个读取的块应用
strtolower()
,使其不区分大小写(我想这可以避免一些问题…)。但为了简洁起见,我省略了这一点再次感谢您的编辑,但不幸的是,
函数prettify(&$value)没有得到任何更改{
并在$value`中更改了第二个
$inStr`。我也尝试过不使用我的函数,但没有任何更改。我认为这可能是一个有效的解决方案。2个问题:使用
全局
是否是一个好方法?将函数放在哪里?我应该在
$value
中重命名
$inStr
。您的示例与我的示例不同。Every单词与“-”分开,如示例“hello und me no das-&der die”。请添加示例输出,如echo prettify(“oeffnung boerse und oekologie”);该输出应为:“Öffnung Böerse undÖkologie。函数可能需要“UTF-8”的某个位置.Q1:当然不是!!如果你可以选择的话,即使使用php本身也不是个好主意:-P.我只是没有太多时间,不得不在高效和优雅之间做出选择;-)。或者你可以
"use strict";
var prettify = (function(){
    // Notice, this is only internally visible scope.
    var noUpper = {};
    for (
        let k of ['und','das','der','die']
    ) noUpper[k] = true;
    function htmlentities(str){
        // There is no native php htmlentities() equivalent in javascript.
        // So should implement it or rely on existing implementations in external
        // modules such underscore.
        // I left it empty because it is only for explanatory purposes.
        return str.replace("&", "&amp;"); // (Just to match PHP example...)
    };

    // ...and below function would be the only thing visible (exposed) outside.
    return function prettify_implementation(inStr){
        return htmlentities(inStr
            .split("-")
            .map(function(chunk){
                chunk = chunk.toLowerCase();
                return noUpper[chunk]
                    ? chunk
                    : chunk.substring(0, 1).toUpperCase()
                        +chunk.substring(1)
                ;       
            })  
            .join(" ")
        );  

    };
})();

console.log(prettify("hello-und-me-no-das-&-der-die")); // Sorry: I don't understand German ;-)

// Output: "Hello und Me No das &amp; der die"