Jump to content

[SOLVED] regex question, what to do about special characters?


M.O.S. Studios

Recommended Posts

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

Link to comment
Share on other sites

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 ].

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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).

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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)

Link to comment
Share on other sites

$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.

Link to comment
Share on other sites

...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})?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.