I need a regex to remove the digits from a string but not also the spaces. I currently have
$city_location = 'UK,0113|Leeds new york'; $sip_city = '0113Leeds new york'; $city = preg_replace('/[^a-z]/i', '', $sip_city);
It removes the digits but also the spaces so I need a regex which won't remove the spaces.
Use \\d
if you want to remove all digits
$city = preg_replace('/\d/', '', $sip_city);
Or [^az\\s]
if you want to replace all except alphabets and whitespaces
$city = preg_replace('/[^a-z\s]/i', '', $sip_city);
use
$city = preg_replace('/[0-9]/', '', $sip_city);
in your code the regex engine
doesnt match anything that is not in the alphabet that is AZ
and az
. so spaces are not in the alphabet and they get matched. i dont have much experience with regex but one thing which i have understood is that
it is better to tell the regex engine what u want rather than telling what u dont want
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.