Jump to content

Allow Only One Hyphen with a Literal String


equipment

Recommended Posts

Please tell us how you would like to use this?

 

Is there input from the user by post data for example?

 

if(preg_match_all('/-/', $the_string, $matches)<2){

}

The manual says you only need two parameters for preg_match_all, and that you don't need to give a $matches parameter, but my server sure didn't like that, maybe yours do?

Well it is just like that, it is for inputted data through an input field, simply a literal string with maximum one hyphen should be allowed not more, solely alphabetic with no special characters.

 

I just have problems wrapping my head around preg_match

 

example-string

When confronted with a problem like this, it's often a good idea to take a step back and ask WHY you only want one hyphen.  What purpose does that serve?

 

However, this regex solves your issue without asking why:

/(^-[a-z]+$)|(^[a-z]+-[a-z]+$)|(^[a-z]-$)/

 

Also, in the future, please describe the whole problem.  Xyph answered your question in its entirety, and then you came back and said "that's a good start, now here's two more rules." 

  Xyph answered your question in its entirety, and then you came back and said "that's a good start, now here's two more rules."

 

I outlined the rules clearly in the first post:

It should be solely alphabetic (lower case) with no special characters and only one hyphen should be allowed.

 

And then I wrote an example like this, before xyph would post:

 

example-string

 

To make you understand that in marketers terms, I want to build a tag system which should only accept this:

example-string

 

And that is enough one needs to know, simply enough to know, simply enough to know, simply enough to know, simply enough to know, simply enough to know.

pure regex:

 

if ( preg_match('~^[a-z]*-?[a-z]*$~',$string) ) {
  // valid
} else {
  // invalid
}

 

blended...may possibly be a little faster:

 

if  ( (substr_count($string,"-")<=1) &&  (preg_match('~^[a-z-]$~',$string)) )  {
  // valid
} else {
  // invalid
}

Numbers calculated using this benchmarking function.

 

Round 1:

times executed (each): 250000
fastest to slowest:
Array
(
    [josh_pure] => 0.00000539963519708157
    [ManiacDan] => 0.0000057829462635701
    [josh_blended] => 0.0000060079560636085
)
biggest difference time: 0.00000060832086652693
fastest is 11.266% faster than the slowest

 

Round 2:

times executed (each): 250000
fastest to slowest:
Array
(
    [josh_pure] => 0.00000552566420531364
    [ManiacDan] => 0.00000584143936571746
    [josh_blended] => 0.00000610485283878271
)
biggest difference time: 0.00000057918863346907
fastest is 10.4818% faster than the slowest

 

Round 3:

times executed (each): 250000
fastest to slowest:
Array
(
    [josh_pure] => 0.00000537841102728821
    [ManiacDan] => 0.00000559228
    [josh_blended] => 0.00000604077199996
)
biggest difference time: 0.00000066236097267179
fastest is 12.3152% faster than the slowest

18165_.txt

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.