sayedsohail Posted October 2, 2007 Share Posted October 2, 2007 Hi everyone, This is my preg_replace which works fine and replaces all characters other than the set specified within ^a-z0-9, but how do i change this to allow ^a-z0-9 plus (-,._), and single space between the $var value i.e, cat's ltd., UK to cat's ltd., UK $var1 = preg_replace('/([^a-z0-9 ])/i', '', $var); Quote Link to comment https://forums.phpfreaks.com/topic/71514-solved-preg_replace-how-to-allow-a-z0-9-_/ Share on other sites More sharing options...
MadTechie Posted October 2, 2007 Share Posted October 2, 2007 $var1 = preg_replace('/[^a-z0-9 ,.\'-]*/i', '', $var1 ); //clean chars $var1 = preg_replace('/\s{2,}/i', ' ', $var1 );//clean spaces as a note this should be in the RegEx Section Quote Link to comment https://forums.phpfreaks.com/topic/71514-solved-preg_replace-how-to-allow-a-z0-9-_/#findComment-360020 Share on other sites More sharing options...
sayedsohail Posted October 2, 2007 Author Share Posted October 2, 2007 preg_replace('/[^a-z0-9 ,.\'-]*/i', '', $var1 ); can you please explain why we have used ,.\'- and */, please. can't we just use ,.'- instead. Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/71514-solved-preg_replace-how-to-allow-a-z0-9-_/#findComment-360024 Share on other sites More sharing options...
MadTechie Posted October 2, 2007 Share Posted October 2, 2007 your replacing everything except a-z0-9 ,.'- now you have to escape the single quote as were using that to quote the string to ' becomes \' you don't need the * Quote Link to comment https://forums.phpfreaks.com/topic/71514-solved-preg_replace-how-to-allow-a-z0-9-_/#findComment-360027 Share on other sites More sharing options...
sayedsohail Posted October 2, 2007 Author Share Posted October 2, 2007 if you wish to answer sorry what is that * we used for. Quote Link to comment https://forums.phpfreaks.com/topic/71514-solved-preg_replace-how-to-allow-a-z0-9-_/#findComment-360030 Share on other sites More sharing options...
MadTechie Posted October 2, 2007 Share Posted October 2, 2007 just means 0 or more occurances of the previous set.. the fact is you don't need it for that. Quote Link to comment https://forums.phpfreaks.com/topic/71514-solved-preg_replace-how-to-allow-a-z0-9-_/#findComment-360033 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.