Blogger Information
Blog 19
fans 0
comment 1
visits 19548
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
PHP验证字符串中是否包含指定字符串
TANKING的代码日志
Original
673 people have browsed it

1.strpos

strpos函数返回boolean值。FALSE和TRUE不用多说.用 “===”进行判断。strpos在执行速度上都比以上两个函数快,另外strpos有一个参数指定判断的位置,但是默认为空。意思是判断整个字符串。缺点是对中文的支持不好。

  1. if(strpos('www.php.cn','php') !== false){
  2. echo '包含php';
  3. }else{
  4. echo '不包含php';
  5. }

2.explode

用explode进行判断PHP判断字符串的包含。

  1. function check($str){
  2. // 判断是否包含a这个字符
  3. $needle ='a';
  4. $tmparray = explode($needle,$str);
  5. if(count($tmparray)>1){
  6. // 如果包含就返回true
  7. return true;
  8. } else{
  9. return false;
  10. }
  11. }
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments