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?

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

  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.

Link to comment
Share on other sites

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

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.