代码之家  ›  专栏  ›  技术社区  ›  kibus90

如何删除最后一个破折号后的字符串(但在某些文本或没有特殊格式文本时跳过破折号)?

  •  1
  • kibus90  · 技术社区  · 6 年前

    我有密码:

    <?php $stringx = get_the_title();
    echo preg_replace('/-[^-]*$/', '', $stringx); ?>
    

    这个很好,但我想让它更精确一点。。。

    我的弦乐是:

    Production worker - Eindhoven, Netherlands
    Production worker - fish - Eindnhoven, Netherlands
    Production worker - fish
    Porduction worker - fish - Bunschoten-Spakenburg, Netherlands
    

    最后一次破折号后总是有一条线。。。。

    Production worker
    Production worker - fish
    Production worker
    Porduction worker - fish - Bunschoten
    

    所以我接错了线。。。。

    但我想得到:

    Production worker
    Production worker - fish
    Production worker - fish
    Porduction worker - fish
    

    如果:

    • 字符串包含:

    […]破折号[…]和[a-z],[空格]a-z

    最后冲刺后切 ]

    其他:

    • 最后一个破折号在一些文本之间,例如单词 [然后在第二次冲刺后切下]

    - (sometext) a-z,[space]A-Z
    

    我希望是克里尔。。。我试着做匹配正则表达式,但我不能处理它。我没有得到regex并将其与HTML混合。

    3 回复  |  直到 6 年前
        1
  •  3
  •   Toto    6 年前
    $tests = [
        'Production worker - Eindhoven, Netherlands',
        'Production worker - fish - Eindnhoven, Netherlands',
        'Production worker - fish',
        'Porduction worker - fish - Bunschoten-Spakenburg, Netherlands ',
    ];
    
    foreach ($tests as $test) {
        $res = preg_replace('/^.+\K-\h+[A-Z].*$/', '', $test);
        echo $res,"\n";
    }
    

    说明:

    ^           : beginning of line
        .+      : 1 or more any character but newline, greedy
        \K      : forget all we have seen untill this positin
        -       : a dash
        \h+     : 1 or more horizontal spaces
        [A-Z]   : an uppercase
        .*      : 0 or more any character but newline
    $           : end of line
    

    Production worker 
    Production worker - fish 
    Production worker - fish
    Porduction worker - fish 
    
        2
  •  1
  •   Jeff    6 年前

    正如我所说,我认为你不能在一行regex中做到这一点(@Toto证明了我在这里是错的……)。所以我写了一个函数来满足你的需要。只测试给定的输入值。

    <?php
    function reduceItem($item) {
        $pieces = explode(" - ", $item);
    
        // check if last piece fits the pattern "bar, foo"
        $last = $pieces[count($pieces)-1];
                         // this regex can use simplification or refinement
        if(preg_match('/[a-zA-Z]*, [a-zA-Z]*/', $last, $matches)) {
            // if so, cut the last piece out of the array
            array_splice($pieces, count($pieces)-1);
        }
        // put the string back together
        return implode(" - ", $pieces);
    }
    
    // USAGE
    // your test data
    $list = ["Production worker - Eindhoven, Netherlands",
        "Production worker - fish - Eindnhoven, Netherlands",
        "Production worker - fish",
        "Porduction worker - fish - Bunschoten-Spakenburg, Netherlands"];
    
    foreach($list as $item) {
        $newItem = reduceItem($item);
        echo "<br>$newItem";
    }
    
    // Output:
    // Production worker
    // Production worker - fish
    // Production worker - fish
    // Porduction worker - fish
    

    或者使用变量:

    <?php $stringx = get_the_title();
    echo reduceItem($stringx); ?>
    
        3
  •  0
  •   Inazo    6 年前

    <?php
    
    $stringx = get_the_title();
    
    
    $exploded = explode('-',$stringx);
    
    $i=0;
    $to_return = '';
    if( count($exploded) > 1 ){
    
            while( $i < (count($exploded) - 1) ){
    
                    $to_return .= $exploded[$i].' - ';
                    $i++;
            }
    
            $to_return = rtrim( $to_return,' - ');
    }
    else
            $to_return = $stringx;
    
    echo $to_return;
    
    
    ?>