开发者

PHP preg_replace() with exclusions

开发者 https://www.devze.com 2023-02-13 13:04 出处:网络
I\'m trying to replace such string: Karcher HDS-C 7/11, 9/15, 8/15-E with HDS-C 7/11, 9/15, 8/15-E. I use this pattern /[^A-Z0-9\\s\\.\\,\\-\\/\\(\\)]/ 开发者_如何学运维for preg_replace.

I'm trying to replace such string: Karcher HDS-C 7/11, 9/15, 8/15-E with HDS-C 7/11, 9/15, 8/15-E.

I use this pattern /[^A-Z0-9\s\.\,\-\/\(\)]/ 开发者_如何学运维for preg_replace. Instead of getting HDS-C 7/11, 9/15, 8/15-E, I'm getting K HDS-C 7/11, 9/15, 8/15-E with leading K.

So my rule: leave only words (1+ letters) in uppercase, 0-9, special chars; exclude words (2+ letters) that contain lowercase (first letter can be uppercase).

More examples (input => output):

Karcher B 140 R Bp => B 140 R Bp

Yard-Man YM 84 M-W 31AY97KV643 => YM 84 M-W 31AY97KV643

How can I adjust my pattern to get it work?

I also need to filter 1+ leading words in lowercase (with possible first letter in uppercase).

For example:

Karcher Karcher B 140 R Bp => B 140 R Bp

Karcher Karcher Karcher B 140 R Bp => B 140 R Bp


How about targeting the words at the beginning, instead of trying to exclude everything else?

e.g.: '/^(?:[A-Z][a-z-]+[A-Za-z-]* ?)+/'


Maybe try to match everything after first whitespace ?

/.*? (.*)/
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号