PHP preg_replace() 只保留大写单词
如何使用 preg_replace 仅保留大写字母、数字、特殊字符或首字母大写但包含不超过 3 个字符的单词。
例如:
Portocjnk Karaer HDS-C 7/11、9/15、8/15-E => HDS-C 7/11、9/15、8/15-E
Karcher凯驰 B 140 R Bp => B 140 R Bp
Karcher Karcher B 140 R Bsp Trr => B 140 R Bsp Trr
Tatata Tatat Yard-Man YM 84 MW 31AY97KV643 => YM 84 MW 31AY97KV643
(塔塔塔)(塔塔塔)Yard-Man YM 84 MW 31AY97KV643 => YM 84 MW 31AY97KV643
提前致谢。
How can I leave only words in uppercase, digits, special characters, or words where the first letter in uppercase, but it contains no more than 3 characters, with preg_replace.
For example:
Portocjnk Karaer HDS-C 7/11, 9/15, 8/15-E => HDS-C 7/11, 9/15, 8/15-E
Karcher Karcher B 140 R Bp => B 140 R Bp
Karcher Karcher B 140 R Bsp Trr => B 140 R Bsp Trr
Tatata Tatat Yard-Man YM 84 M-W 31AY97KV643 => YM 84 M-W 31AY97KV643
(Tatata) (Tatat) Yard-Man YM 84 M-W 31AY97KV643 => YM 84 M-W 31AY97KV643
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这个适用于你的大部分示例
this one would work with most of your example
这将是一种简单化的白名单方法。这将首先提取所需的部分,而不是 preg_replacing。之后 $result 数组需要重新合并。
您可能需要在第二个
[...]
字符类中添加更多“特殊”字符。查看 https://stackoverflow.com/questions/ 89718/is-there-anything-like-regexbuddy-in-the-open-source-world 获取一些可能有助于设计正则表达式的好工具。
This would be a simplistic whitelist approach. Instead of preg_replacing this will first extract the desired parts. And afterwards the $result array needs to be remerged.
You might need to add some more of the "special" characters in the second
[...]
character class.Check out https://stackoverflow.com/questions/89718/is-there-anything-like-regexbuddy-in-the-open-source-world for some nice tools that might help in designing regular expression.