nish.patel Posted November 19, 2009 Share Posted November 19, 2009 Hi all, I am a newbie so please go gentle... I am trying to get a function to work for my application i have been learning PHP by watch Kevin Skoglund's videos etc but I cannot find a solution for this. I have been googling frantically but no joy hence you guys are my only option now. Below is the snippet of code that I have tried to get it to work but no joy can anyone show me how its done thanks... what i am trying to do is to input "BA'3Ndf$%^A&*(nN)A" and trying to return BANANA in PHP: function remove_nonAtoZ($input) { $input = preg_replace('#[^\d]#', $input); $input = "BA'3Ndf$%^A&*(nN)A"; $output = "BANANA"; return $output; } Quote Link to comment https://forums.phpfreaks.com/topic/182149-php-funtion-to-remove-non-a-to-z/ Share on other sites More sharing options...
Alex Posted November 19, 2009 Share Posted November 19, 2009 ~[^A-Z]~ will work. function remove_nonAtoZ($input) { return preg_replace('~[^A-Z]~', '', $input); } edit: In preg_replace you also need to specify the replacement, which you forgot. Quote Link to comment https://forums.phpfreaks.com/topic/182149-php-funtion-to-remove-non-a-to-z/#findComment-961013 Share on other sites More sharing options...
thebadbad Posted November 19, 2009 Share Posted November 19, 2009 Look up preg_replace() in the manual, it takes three parameters at minimum. Quote Link to comment https://forums.phpfreaks.com/topic/182149-php-funtion-to-remove-non-a-to-z/#findComment-961016 Share on other sites More sharing options...
nish.patel Posted November 19, 2009 Author Share Posted November 19, 2009 wow thanks for the quick reply... I have tried this and get a blank screen: function remove_nonAtoZ($input) { return preg_replace('~[^A-Z]~', '', $input); $input = "BA'3Ndf$%^A&*(nN)A"; $output = "BANANA"; return $output; } [\code] Quote Link to comment https://forums.phpfreaks.com/topic/182149-php-funtion-to-remove-non-a-to-z/#findComment-961019 Share on other sites More sharing options...
rajivgonsalves Posted November 19, 2009 Share Posted November 19, 2009 it should have been <?php function remove_nonAtoZ($input) { return preg_replace('#[^A-Z]#', '', $input); } $input = "BA'3Ndf$%^A&*(nN)A"; echo remove_nonAtoZ($input); ?> Quote Link to comment https://forums.phpfreaks.com/topic/182149-php-funtion-to-remove-non-a-to-z/#findComment-961023 Share on other sites More sharing options...
Alex Posted November 19, 2009 Share Posted November 19, 2009 You should do some research about how functions work and how to utilize them, check out the manual. The way to use this function would be like so: function remove_nonAtoZ($input) { return preg_replace('~[^A-Z]~', '', $input); } echo remove_nonAtoZ("BA'3Ndf$%^A&*(nN)A"); Quote Link to comment https://forums.phpfreaks.com/topic/182149-php-funtion-to-remove-non-a-to-z/#findComment-961024 Share on other sites More sharing options...
nish.patel Posted November 19, 2009 Author Share Posted November 19, 2009 thank you all for reply this is great help for a bright path to learn php and their functions Quote Link to comment https://forums.phpfreaks.com/topic/182149-php-funtion-to-remove-non-a-to-z/#findComment-961178 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.