简体   繁体   中英

How to trim out the space between non letters with PHP?

说,应该保留字母之间的空格(az,不区分大小写)并删除非字母之间的空格吗?

这应该工作:

$trimmed = preg_replace('~([a-z0-9])\s+([a-z0-9])~i', '\1\2', $your_text);

This will strip any whitespace that is between two non-alpha characters:

preg_replace('/(?<![a-z])\s+(?![a-z])/i', '', $text);

This will strip any whitespace that has a non-alpha character on either side (big difference):

preg_replace('/(?<![a-z])\s+|\s+(?![a-z])/i', '', $text);

By using negative look-ahead and negative look-behind assertions, the beginning and end of the string are treated as non-alpha as well.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM