M.O.S. Studios Posted February 3, 2009 Share Posted February 3, 2009 Hey guys, I’m working on a shopping cart that allows a user to input a custom formula to calculate shipping prices. The script works fine the only problem I’m having is ensuring that a user only uses terms that i allow him to enter. To do this I’m using Preg_match and comparing the result to the original. This is what I want preg match to check for ( , ) , [x] , [/] , [+] , [-] , [%] , [/%] , [#] , [sku] , [st] The commas are only to help you visually After doing some reading on regex I realized that allot of those are special characters, can regex still be used with preg_match? Thanks in advance Quote Link to comment https://forums.phpfreaks.com/topic/143658-solved-regex-question-what-to-do-about-special-characters/ Share on other sites More sharing options...
nrg_alpha Posted February 3, 2009 Share Posted February 3, 2009 Hmm.. something tells me you need to provide some valid entries to help us gauge what exactly you are looking for...otherwise, off the top of my head, given your characer list, this is what I come up with (and yes, I have a feeling this will be incorrect, as I sense there is more to all this than you have explained): // $str = input from user if(!preg_match('#^(?:[()]|\[(?:/?%|[/-]|sku|st)\])+$#', $str)){ // Error } I am using ^ and $ to denote from beginning of string to end of string... but since there is no numbers in your explanation, I did not include them..(but again, I have a feeling there is more to it). Please provide complete valid entries the user might input which utilize some of those characters (especially [sku] and [st]).. Oh, and I am assuming you don't mean character classes when you list [sku], but rather a [, followed by sku and then closed off with ]. Quote Link to comment https://forums.phpfreaks.com/topic/143658-solved-regex-question-what-to-do-about-special-characters/#findComment-753761 Share on other sites More sharing options...
M.O.S. Studios Posted February 3, 2009 Author Share Posted February 3, 2009 here would be an example here is my legand [x]=Times, [/]=Divide, [+]=Plus, [-]=Minus, [%]Amount[/%]=Percent, [#]=Number of total Products, [sku]=Number of item numbers, [st]=Sub Total this would be a formula where the shipping is 10% of the subtotal(price befor taxes or shipping or any disscounts). [st][x][%]10[/%] how ever to get this to work i have to use eval(), so i dont want ppl to type in a command that could dammage the website. I'm going to try what you had suggested, unless you think you have a better suggestion? Quote Link to comment https://forums.phpfreaks.com/topic/143658-solved-regex-question-what-to-do-about-special-characters/#findComment-753770 Share on other sites More sharing options...
nrg_alpha Posted February 3, 2009 Share Posted February 3, 2009 My suggestion will not work, based on my suspicions.. I would urge you to have a look here. I am trying to understand your legend.. and I'm still confused. You have =Times (there is nothing on the left hand side of the equal sign), same thing goes for Plus and Number of total Products.. all have nothing on the left side of the equation. So aside from this lack of information, you still haven't provided some actual sample strings of what is acceptable. Once the legend is fully and completely understandable, this is half the battle.. much like I can say the english language is comprised of a-zA-Z,!.? etc.. but doesn't explain how they're used. Please illustrate some complete entries that a user might enter (which is valid). Quote Link to comment https://forums.phpfreaks.com/topic/143658-solved-regex-question-what-to-do-about-special-characters/#findComment-753777 Share on other sites More sharing options...
M.O.S. Studios Posted February 3, 2009 Author Share Posted February 3, 2009 Hey sorry about that, i didn't notice that the phpfreaks website had messed up my leggend, i altered my last comment as to fix the problem Quote Link to comment https://forums.phpfreaks.com/topic/143658-solved-regex-question-what-to-do-about-special-characters/#findComment-753780 Share on other sites More sharing options...
nrg_alpha Posted February 3, 2009 Share Posted February 3, 2009 here is my legand [x]=Times, [/]=Divide, [+]=Plus, [-]=Minus, [%]Amount[/%]=Percent, [#]=Number of total Products, [sku]=Number of item numbers, [st]=Sub Total this would be a formula where the shipping is 10% of the subtotal(price befor taxes or shipping or any disscounts). [st][x][%]10[/%] Ah, it's been edited..ok.. a a little clearer now.. so let me see if I have this straight (given your example).. valid entires would be: [st] (or [sku]?) [x] (or [/] or [+] or [-]) [%] some number [/%]... is this correct? Quote Link to comment https://forums.phpfreaks.com/topic/143658-solved-regex-question-what-to-do-about-special-characters/#findComment-753787 Share on other sites More sharing options...
nrg_alpha Posted February 3, 2009 Share Posted February 3, 2009 Hey sorry about that, i didn't notice that the phpfreaks website had messed up my leggend, i altered my last comment as to fix the problem Yeah, I just realized it myself with my last response.. in this case, you can encapsulate your legen with [ nobbc]......[ /nobbc] (without spaces in the tags) Quote Link to comment https://forums.phpfreaks.com/topic/143658-solved-regex-question-what-to-do-about-special-characters/#findComment-753791 Share on other sites More sharing options...
M.O.S. Studios Posted February 3, 2009 Author Share Posted February 3, 2009 yeah thats right and numbers 0-9 would be ok aswell (i forgot to mention that) Quote Link to comment https://forums.phpfreaks.com/topic/143658-solved-regex-question-what-to-do-about-special-characters/#findComment-753802 Share on other sites More sharing options...
.josh Posted February 3, 2009 Share Posted February 3, 2009 perhaps preg_quote would be of some use to you? Quote Link to comment https://forums.phpfreaks.com/topic/143658-solved-regex-question-what-to-do-about-special-characters/#findComment-753806 Share on other sites More sharing options...
nrg_alpha Posted February 3, 2009 Share Posted February 3, 2009 $str = array('[st][x][%]7[/%]','[sku][/][%]23[/%]','[sku][+][%]10[/%]','[sku][-][%]23.34[/%]','[st][/][%]7[%]','[sku][x][%]h[/%]', '[sku][x][%]12.567898[/%]'); foreach($str as $arr){ if(preg_match('#^\[s(?:t|ku)\]\[[x/+-]\]\[%\]\d+(\.\d{2})?\[/%\]$#', $arr)){ echo $arr . ' - Success! Correct format!' . "<br />\n"; } else { echo $arr . ' - Error.. invalid format!' . "<br />\n"; } } Is this along the lines you are looking for? (I added the optional decimal and 2 numbers afterward... if you dont want decimals, then simply remove (\.\d{2})? from the pattern. If you want decimals and don't care how many there are, you can use (\.\d+)? instead in the pattern. Quote Link to comment https://forums.phpfreaks.com/topic/143658-solved-regex-question-what-to-do-about-special-characters/#findComment-753819 Share on other sites More sharing options...
nrg_alpha Posted February 3, 2009 Share Posted February 3, 2009 yeah thats right and numbers 0-9 would be ok aswell (i forgot to mention that) This is why people should collect all the needed information prior to posting, so as to make everything clear from the getgo. Quote Link to comment https://forums.phpfreaks.com/topic/143658-solved-regex-question-what-to-do-about-special-characters/#findComment-753823 Share on other sites More sharing options...
M.O.S. Studios Posted February 3, 2009 Author Share Posted February 3, 2009 sorry for that, its my mistake, i dont usaly forget info like that, but im doing this while working on other parts of the site because my dead line is REALY SOON! lol thanks im going to use that code Quote Link to comment https://forums.phpfreaks.com/topic/143658-solved-regex-question-what-to-do-about-special-characters/#findComment-753828 Share on other sites More sharing options...
nrg_alpha Posted February 3, 2009 Share Posted February 3, 2009 ...im doing this while working on other parts of the site because my dead line is REALY SOON! Wait a minute...dead line? Am I doing your homework, job or something? EDIT - come to think of it, that last optional bit should be non capturing.. (?:\.\d{2})? Quote Link to comment https://forums.phpfreaks.com/topic/143658-solved-regex-question-what-to-do-about-special-characters/#findComment-753832 Share on other sites More sharing options...
M.O.S. Studios Posted February 3, 2009 Author Share Posted February 3, 2009 no no no, im working on a website, and i wanna have it up for the end of the week, im starting my own company and im building my own website. at lease untill i can hire some one to do it for me Quote Link to comment https://forums.phpfreaks.com/topic/143658-solved-regex-question-what-to-do-about-special-characters/#findComment-753842 Share on other sites More sharing options...
M.O.S. Studios Posted February 3, 2009 Author Share Posted February 3, 2009 Awesome, i didn't use your code because it didn't need to be a loop, how ever the regex cod was spot on thanks sooooo much! Quote Link to comment https://forums.phpfreaks.com/topic/143658-solved-regex-question-what-to-do-about-special-characters/#findComment-753855 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.