1

我有那个代码:

<?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

因此,如果:

  • 字符串包含:

[...]DASH[...] 和 [az],[space]AZ

[然后在最后一个破折号后剪切]

否则:

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

但是,如果文本不包含最后一个破折号后的文本,则不要剪切:

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

我希望它是真实的......我尝试匹配正则表达式,但我无法处理它。我没有得到正则表达式并将其与 HTML 混合。

4

3 回答 3

3
$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 
于 2018-08-23T15:55:57.747 回答
1

正如我所说,我认为您不能在单行正则表达式中做到这一点(@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); ?>
于 2018-08-23T15:47:24.520 回答
0

我会对其进行测试并提供您需要的字符串,如果您不想要一些解释,请随时询问我们

<?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;


?>
于 2018-08-23T15:43:39.310 回答