Home > Backend Development > PHP Tutorial > 求段php正则代码验证字符串解决思路

求段php正则代码验证字符串解决思路

WBOY
Release: 2016-06-13 10:07:12
Original
1056 people have browsed it

求段php正则代码验证字符串
字符串规则:可以纯数字或纯字母 亦可二者结合,只能使用“-”不能使用汉字、其他字符符号并且字符串开头不能为“-”,结尾也不能为“-”

------解决方案--------------------

PHP code
preg_match('/^[a-z0-9]+[a-z0-9-]*[a-z0-9]+$/i',$string);<br><font color="#e78608">------解决方案--------------------</font><br>我只管挑错,<br><br>1楼match不了一个字符的:)<br>2楼也是,\w还包含_, 楼主不要的<br><font color="#e78608">------解决方案--------------------</font><br>1楼的正则稍微修改下,适应一个字符的情况:<br>
Copy after login
PHP code
/^[a-z0-9]+[a-z0-9-]*[a-z0-9]+|[a-z0-9]+$/i<br><font color="#e78608">------解决方案--------------------</font><br>$str='\w+[0-9a-zA-Z\-]+\w$'<br><font color="#e78608">------解决方案--------------------</font><br>我也来凑个热闹<br>
Copy after login
PHP code
/^[0-9a-z][-0-9a-z]*[0-9a-z]$/i<div class="clear">
                 
              
              
        
            </div>
Copy after login
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template