Jump to content

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


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

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

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?

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

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?

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)

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

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.

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

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

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.