chrisprse Posted April 20, 2009 Share Posted April 20, 2009 Hello I'm currently using CodeIgniter and I want to extend the validation library. I am using the following callback function in my forms: function alpha_numeric($str) { return ( ! preg_match("/^([a-z0-9])+$/i", $str)) ? FALSE : TRUE; } This checks whether or not the field contains alphanumeric characters and returns TRUE or FALSE. I need to create a new function that will ONLY ALLOW the following characters: @£$¥èéùìòÇØøÅåΔ_ΦΓΛΩΠΨΣΘΞÆæßÉ !"#¤%&'()*+,-./0123456789:;<=>?¡ABCDEFGHIJKLMNOPQRSTUVWXYZÄÖÑܧ¿abcdefghijklmnopqrstuvwxyzäöñüà^}{\[~]|€ If the string contains any other character it will return FALSE! I am aware that the metacharacters need to be escaped which leaves me with the following: @£\$¥èéùìòÇØøÅåΔ_ΦΓΛΩΠΨΣΘΞÆæßÉ !"#¤%&'\(\)\*\+,-\./0123456789:;<=>\?¡ABCDEFGHIJKLMNOPQRSTUVWXYZÄÖÑܧ¿abcdefghijklmnopqrstuvwxyzäöñüà\^}{\\\[~]\|€ Now, this is where I get stuck! Is anyone able to help me create a new function based on the above "alpha_numeric" function to help me achieve my goal? If this new function returns TRUE I would then like to run it through another check which adds +1 to a variable called $counter for every occurrence of any the following characters: ^}{\[~]|€ I am so stuck on this it's unbelievable and any help will be GREATLY appreciated! Thanks very much in advance. Regards Chris P.S. I am also after another two functions based on the above "alpha_numeric" function. The first: Strings must be between 3 & 16 characters in length and can only comprise alphanumeric characters or the hyphen (-) character. They cannot begin or end with a hyphen. The second: Strings must be up to 11 characters starting with a letter and consisting of letters, numbers, spaces, full stops and hyphens. I have exactly $10.68 USD in my PayPal account and I'll happily "donate" this to anyone that can help. I know there are probably some kind people out there willing to do this for free but I'll happily make a PayPal payment to the first person to reply. It's my way of buying you a bottle of wine via the web! Thanks Chris Link to comment https://forums.phpfreaks.com/topic/154945-regex-with-custom-character-sets/ Share on other sites More sharing options...
Daniel0 Posted April 21, 2009 Share Posted April 21, 2009 I've attached a file containing three functions. For some reason SMF didn't like the unicode symbols, so I couldn't post it here. You can donate your money to PHP Freaks if you wish. [attachment deleted by admin] Link to comment https://forums.phpfreaks.com/topic/154945-regex-with-custom-character-sets/#findComment-815236 Share on other sites More sharing options...
chrisprse Posted April 21, 2009 Author Share Posted April 21, 2009 Wow!!!! Thank you SO MUCH! That is great, thank you! I am just about to make a donation to PHP Freaks! I will post confirmation of the donation shortly! Thanks again Chris :) Link to comment https://forums.phpfreaks.com/topic/154945-regex-with-custom-character-sets/#findComment-815406 Share on other sites More sharing options...
Daniel0 Posted April 21, 2009 Share Posted April 21, 2009 You could purchase a 1-month supporter subscription using your profile: http://www.phpfreaks.com/forums/index.php?action=profile;area=subscriptions That's $10. Link to comment https://forums.phpfreaks.com/topic/154945-regex-with-custom-character-sets/#findComment-815411 Share on other sites More sharing options...
chrisprse Posted April 21, 2009 Author Share Posted April 21, 2009 Thank you Daniel - I certainly will. Once the first function returns TRUE I would then like to run another check which adds +1 to a variable called $counter for every occurrence of any the following characters: ^}{\[~]|€ So say if I had the following string: Hello! My name is [Chris]. My currency is the euro (€). The $counter variable would be: 3 Could this be achieve using Regex or would I be better off looking at string functions? Regards Chris Link to comment https://forums.phpfreaks.com/topic/154945-regex-with-custom-character-sets/#findComment-815426 Share on other sites More sharing options...
chrisprse Posted April 21, 2009 Author Share Posted April 21, 2009 This is what I came up with but I can't help but think there must be a more effective way? <?php $string = "Hello! My name is [Chris]. My currency is the euro (€)."; $counter = 0; $characters = array("^", "}", "{", "\\", "[", "~", "]", "|", "€"); foreach($characters as $character) { $counter = $counter + substr_count($string, $character); } echo $counter; ?> Regards Chris Link to comment https://forums.phpfreaks.com/topic/154945-regex-with-custom-character-sets/#findComment-815446 Share on other sites More sharing options...
Daniel0 Posted April 21, 2009 Share Posted April 21, 2009 Hmm... you could probably do this: function countContains($string, $chars) { preg_match_all('#[' . preg_quote($chars, '#') . ']#', $string, $matches); print_r($matches); } Link to comment https://forums.phpfreaks.com/topic/154945-regex-with-custom-character-sets/#findComment-815455 Share on other sites More sharing options...
Daniel0 Posted April 21, 2009 Share Posted April 21, 2009 Uhm... that print_r() shouldn't have been there... should have been: function countContains($string, $chars) { preg_match_all('#[' . preg_quote($chars, '#') . ']#', $string, $matches); return count($matches[0]); } Sorry. Link to comment https://forums.phpfreaks.com/topic/154945-regex-with-custom-character-sets/#findComment-815468 Share on other sites More sharing options...
chrisprse Posted May 10, 2009 Author Share Posted May 10, 2009 Hi there I'm having trouble with the following function: if(preg_match('#[^' . preg_quote('@£$¥èéùìòÇØøÅåΔ_ΦΓΛΩΠΨΣΘΞÆæßÉ !"#¤%&\'()*+,-./:;<=>?¡ÄÖÑܧ¿äöñüà^}{\[~]|€', '#') . 'A-Za-z0-9]#i', $message)) { echo "Good!"; } else { echo "Bad!"; } Basically, I am taking input from a textarea ($message) which can contain only the following characters: @£$¥èéùìòÇØøÅåΔ_ΦΓΛΩΠΨΣΘΞÆæßÉ !"#¤%&\'()*+,-./:;<=>?¡ÄÖÑܧ¿äöñüà^}{\[~]|€A-Za-z0-9 If the text contains a combination of ONLY these characters it echo's "Good!". If it contains ANY OTHER characters such as © it would echo "Bad!" I can't figure out where I am going wrong - is anyone able to help? Much appreciated - Chris Link to comment https://forums.phpfreaks.com/topic/154945-regex-with-custom-character-sets/#findComment-831053 Share on other sites More sharing options...
chrisprse Posted May 10, 2009 Author Share Posted May 10, 2009 I think the problem is because there are wierd characters in there. I had to use mb_strlen to get the string length of the message. Is there anyway to correct the above to work with these characters. I've seen mb_ereg but not sure how to implement it - is anyone able to shed some light? Cheers Chris Link to comment https://forums.phpfreaks.com/topic/154945-regex-with-custom-character-sets/#findComment-831203 Share on other sites More sharing options...
rea|and Posted May 12, 2009 Share Posted May 12, 2009 Uhm... that print_r() shouldn't have been there... should have been: function countContains($string, $chars) { preg_match_all('#[' . preg_quote($chars, '#') . ']#', $string, $matches); return count($matches[0]); } Sorry. Hello, it's just a side issue, but you could rewrite it in this way: function countContains($string, $chars){ return preg_match_all('#[' . preg_quote($chars, '#') . ']#', $string, $matches); } Link to comment https://forums.phpfreaks.com/topic/154945-regex-with-custom-character-sets/#findComment-832216 Share on other sites More sharing options...
xylex Posted May 15, 2009 Share Posted May 15, 2009 You need to turn on the UTF8 modifier in your pattern. if(preg_match('#[^' . preg_quote('@£$¥èéùìòÇØøÅåΔ_ΦΓΛΩΠΨΣΘΞÆæßÉ !"#¤%&\'()*+,-./:;<=>?¡ÄÖÑܧ¿äöñüà^}{\[~]|€', '#') . 'A-Za-z0-9]#iu', $message)) { echo "Good!"; } else { echo "Bad!"; } Note the "u" at #iu', $message) Link to comment https://forums.phpfreaks.com/topic/154945-regex-with-custom-character-sets/#findComment-834589 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.