PHP语言是一个功能强大的嵌入式HTML脚本语言,它的易用性让许多程序员选择使用。PHP判断字符串的包含,可以使用PHP的内置函数 strstr,strpos,stristr直接进行判断.也可以通过explode函数的作用写一个判断函数。下面介绍PHP判断字符串的包含的具体使 用方法:

1. strstr: 返回一个从被判断字符开始到结束的字符串,如果没有返回值,则不包含

代码如下:
< ?php
/*如手册上的举例*/
$email = ‘ <script></script>user@example.com <script></script>’;
$domain = strstr($email, ‘@’);
echo $domain;
// prints @example.com
?>

2. stristr: 它和strstr的使用方法完全一样.唯一的区别是stristr不区分大小写.

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

PHP判断字符串的包含代码如下:
$str= ‘abc’;
$needle= ‘a’;
$pos = strpos($str, $needle);

上面也能得出正确的结果,但方法是错误的,

如果if(strpos($str,$needle) ),那就得不到正确的结果,

原因是位置是从0开始,第一个位置找到了,就是0,php中的0,也就不是true,上面的判断将不会成立,这点要十分注意!

4. 用explode进行判断PHP判断字符串的包含代码如下:
function checkstr($str){
$needle = “a”;//判断是否包含a这个字符
$tmparray = explode($needle,$str);
if(count($tmparray)>1){
return true;
} else{
return false;
}
}
以上就是PHP判断字符串的包含的具体代码示例。in_array — 检查数组中是否存在某个值例子 1. in_array() 例子
"Got Irix";}
if (
in_array("mac", $os)) {echo"Got mac";}
?>
第二个条件失败,因为 in_array() 是区分大小写的,所以以上程序显示为:Got Irix
例子 2. in_array() 严格类型检查例子
if (
in_array(1.13, $a, true))
{echo"1.13 found with strict check\n";}
?>
上例将输出:
1.13 found with strict check例子 3. in_array() 中用数组作为 needle

= array(array('p', 'h'), array('p', 'r'), 'o');if (
in_array(array('p', 'h'), $a)) {echo
"'ph' was found\n";}
if (
in_array(array('f', 'i'), $a)) {echo
"'fi' was found\n";}
if (
in_array('o', $a)) {echo
"'o' was found\n";}
?>

上例将输出:

'ph' was found
'o' was found

array_search — 在数组中搜索给定的值,如果成功则返回相应的键名。

转载https://blog.csdn.net/qq_39110055/article/details/79071264

php 判断字符串包含的更多相关文章

  1. JS判断字符串包含的方法

    本文实例讲述了JS判断字符串包含的方法.分享给大家供大家参考.具体如下: 1. 例子: 1 2 3 4 5 6 7 8 var tempStr = "tempText" ; var ...

  2. lua判断字符串包含另一个字符串

    lua判断字符串包含另一个字符串 --string.find("元字符串","模式字符串") 如下: print(string.find("CCBWe ...

  3. struts2 用if标签判断字符串包含

    String testStr = "用来判断是否包含的字符串"; <s:property value="testStr"/> <s:if te ...

  4. php 判断字符串包含中文(转)

    $str = "测试中文"; echo $str; echo "<hr>"; //if (preg_match("/^[".ch ...

  5. Android java判断字符串包含某个字符段(或替换)

    String str = "; ) { System.out.println("包含该字符串"); }

  6. shell 判断字符串包含的5种方法

    strA="long string" strB="string" result=$(echo $strA | grep "${strB}") ...

  7. 【前端】javascript判断undefined、null、NaN;字符串包含等

    JS中判断null.undefined与NaN的方法 这篇文章主要介绍了JS中判断null.undefined与NaN的方法,需要的朋友可以参考下 . . 写了个 str ="s" ...

  8. Shell判断字符串包含关系的几种方法

    现在每次分析网站日志的时候都需要判断百度蜘蛛是不是真实的蜘蛛,nslookup之后需要判断结果中是否包含“baidu”字符串 以下给出一些shell中判断字符串包含的方法,来源程序员问答网站 stac ...

  9. shell 字符串包含

    转自:Shell判断字符串包含关系的几种方法 现在每次分析网站日志的时候都需要判断百度蜘蛛是不是真实的蜘蛛,nslookup之后需要判断结果中是否包含"baidu"字符串 以下给出 ...

随机推荐

  1. Git 设置 用户名 和 邮箱

    git config --global user.name "Vincent" git config --global user.email "********@qq.c ...

  2. 破解phpStorm 2018 亲测

    网上教程很多,这里我就不多赘述,我也是看其他教程试过来的. 下面分步骤介绍一下: 1.下载,我这里选用的版本是 phpStorm 2018.3 ,下载地址 https://www.newasp.net ...

  3. 经典解压缩软件 WinRAR 5.80 sc 汉化去广告版

    目录 1. 按 2. 提醒 3. 下载地址 1. 按 WinRAR拥有全球超过五千万的用户,是目前最受欢迎的压缩软件, 没有比它更加好的方法来实现高效安全的文件传输,减少电子邮件传输时间,或是迅速压缩 ...

  4. 三、Signalr外部链接

    一.本地外部引用 <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head& ...

  5. C++ STL(二)vector的用法

    ##### vector的定义 ```#include <iostream>#include <string>#include <vector>using name ...

  6. 循环 for 读取文件

    cat filename(待读取的文件) | while read line do echo $line done

  7. gd_t结构 bd_t结构

    gd_t在u-boot-2018.07-fmxx/include/asm-generic/global_data.h中定义 typedef struct global_data {    bd_t * ...

  8. SpirngBoot之整合邮件服务

    一.集成邮件服务 1.1 获取客户端授权码 1.2 引入依赖 <dependencies> ...... <dependency> <groupId>org.spr ...

  9. JAVA语言程序设计-笔记摘录

    JAVA 程序语言设计(基础篇) 笔记摘录 为避免输入错误, 不要在nextByte().nextShort().nextInt()等等后面使用nextLine() nextXXXXX()都称为令牌读 ...

  10. DevExpress WinForms v19.1新版亮点:Spreadsheet/Sunburst控件功能增强

    行业领先的.NET界面控件DevExpress v19.1终于正式发布,本站将以连载的形式介绍各版本新增内容.在本系列文章中将为大家介绍DevExpress WinForms v19.1中新增的一些控 ...