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 [^a-z\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 A-Z
and a-z
. 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