2014-06-09 2 views
0

preg_match와 함께 단일 하이픈 만 허용하려면 어떻게해야합니까?preg_match와 함께 단 하나의 마이너스를 허용하려면 어떻게해야합니까?

저의 예는 작동하지 않습니다. 예를 들어

function isSubdomainValid($subdomain){ 
    $patt = '/^([a-z0-9]+-?[a-z0-9]+[a-z0-9]+)+$/i'; 
    if (preg_match($patt, $subdomain)){ 
     return true; 
    } 
    return false; 
} 

,

helloworld     ok 
hello-world     ok 
hello-world-again   ok 
hello--world     not ok 
hello-world--again--   not ok 
--hello-world--again   not ok 
hello-world--again-   not ok 
-hello-world-again   not ok 
+0

귀하의 예제 코드는 모든 표시에 대해 원하는 결과를 얻을 수 인스턴스. – Armali

답변

1

를 시도 좋아!

function isSubdomainValid($subdomain){ 
    $patt = '/^([a-z0-9]+[a-z0-9-]+[a-z0-9]+)+$/i'; 
    $patt2 = '/-{2}/'; 
    if (preg_match($patt, $subdomain)){ 
     if (!preg_match($patt2, $subdomain)){ 
      return true; 
     } 
    } 
    return false; 
} 
관련 문제