我有那个代码:
<?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 混合。