equipment Posted April 22, 2012 Share Posted April 22, 2012 How could this be achieved with preg_match? It should be solely alphabetic (lower case) with no special characters and only one hyphen should be allowed. Quote Link to comment https://forums.phpfreaks.com/topic/261416-allow-only-one-hyphen-with-a-literal-string/ Share on other sites More sharing options...
MMDE Posted April 22, 2012 Share Posted April 22, 2012 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? Quote Link to comment https://forums.phpfreaks.com/topic/261416-allow-only-one-hyphen-with-a-literal-string/#findComment-1339575 Share on other sites More sharing options...
equipment Posted April 22, 2012 Author Share Posted April 22, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/261416-allow-only-one-hyphen-with-a-literal-string/#findComment-1339698 Share on other sites More sharing options...
xyph Posted April 22, 2012 Share Posted April 22, 2012 <?php $string = 'some----string---with-hyphens----'; $new_string = preg_replace('/-+/', '-', $string); echo $new_string; // some-string-with-hyphens- ?> Quote Link to comment https://forums.phpfreaks.com/topic/261416-allow-only-one-hyphen-with-a-literal-string/#findComment-1339701 Share on other sites More sharing options...
equipment Posted April 22, 2012 Author Share Posted April 22, 2012 xyph, that is a good starting point, how can I now add the following conditions to only have? 1. only lower case letters are possible 2. no other special characters, including white space are possible 3. and only one hyphen is possible Quote Link to comment https://forums.phpfreaks.com/topic/261416-allow-only-one-hyphen-with-a-literal-string/#findComment-1339702 Share on other sites More sharing options...
ManiacDan Posted April 22, 2012 Share Posted April 22, 2012 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." Quote Link to comment https://forums.phpfreaks.com/topic/261416-allow-only-one-hyphen-with-a-literal-string/#findComment-1339706 Share on other sites More sharing options...
equipment Posted April 23, 2012 Author Share Posted April 23, 2012 Double post. Quote Link to comment https://forums.phpfreaks.com/topic/261416-allow-only-one-hyphen-with-a-literal-string/#findComment-1339709 Share on other sites More sharing options...
equipment Posted April 23, 2012 Author Share Posted April 23, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/261416-allow-only-one-hyphen-with-a-literal-string/#findComment-1339711 Share on other sites More sharing options...
.josh Posted April 23, 2012 Share Posted April 23, 2012 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 } Quote Link to comment https://forums.phpfreaks.com/topic/261416-allow-only-one-hyphen-with-a-literal-string/#findComment-1339712 Share on other sites More sharing options...
ManiacDan Posted April 23, 2012 Share Posted April 23, 2012 Josh, is your regex faster than mine? Just wondering. Conditional regexes have always thrown me, despite being an 'expert' otherwise. Quote Link to comment https://forums.phpfreaks.com/topic/261416-allow-only-one-hyphen-with-a-literal-string/#findComment-1339743 Share on other sites More sharing options...
.josh Posted April 23, 2012 Share Posted April 23, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/261416-allow-only-one-hyphen-with-a-literal-string/#findComment-1339756 Share on other sites More sharing options...
ManiacDan Posted April 23, 2012 Share Posted April 23, 2012 Nice, thanks. Quote Link to comment https://forums.phpfreaks.com/topic/261416-allow-only-one-hyphen-with-a-literal-string/#findComment-1339825 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.