Jump to content

Recommended Posts

Hello Everybody!

 

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.

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

 

 

Thanks in advance.

Link to comment
https://forums.phpfreaks.com/topic/229011-preg_replace-with-exclusions/
Share on other sites

I think your pattern is far more complex than is necessary. Think logically. All inputs are cut before the first space character? Use that to your advantage!

 

New pattern:

 

^[^ ]+

 

In code:

 

$input = preg_replace('#^[^ ]+ #', '', $input);

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.