Maison > php教程 > PHP源码 > le corps du texte

PHP 判断是否包含某字符串

PHP中文网
Libérer: 2016-05-25 17:00:16
original
1809 Les gens l'ont consulté

                       

1. [代码]strstr 和 stristr的用法    

strstr: 返回一个从被判断字符开始到结束的字符串,如果没有返回值,则不包含. stristr: 它和strstr的使用方法完全一样.唯一的区别是stristr不区分大小写.

$email = ‘ user@example.com’;
$domain = strstr($email, ‘@’);
echo $domain;
// prints @example.com
Copier après la connexion

2. [代码]strpos的用法

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

    $str= ‘abc’;
    $needle= ‘a’;
    $pos = strpos($str, $needle);
Copier après la connexion

3. [代码]explode 的用法

用explode进行判断PHP判断字符串的包含代码如下:
    function checkstr($str){
    $needle = “a”;//判断是否包含a这个字符
    $tmparray = explode($needle,$str);
    if(count($tmparray)>1){
    return true;
    } else{
    return false;
    }
    }
Copier après la connexion

4. [代码]in_array — 检查数组中是否存在某个值例子

1. in_array() 例子
<?php$os= array("Mac", "NT", "Irix", "Linux");if (in_array("Irix", $os)) {echo
"Got Irix";}
if (
  in_array("mac", $os)) {echo"Got mac";}
?>
第二个条件失败,因为 in_array() 是区分大小写的,所以以上程序显示为:Got Irix


2. in_array() 严格类型检查例子
<?php$a= array(&#39;1.10&#39;, 12.4, 1.13);if (in_array(&#39;12.4&#39;, $a, true)) {echo"&#39;12.4&#39; found with strict check\n";}
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
<?php$a

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

上例将输出:
&#39;ph&#39; was found
&#39;o&#39; was found


array_search — 在数组中搜索给定的值,如果成功则返回相应的键名
Copier après la connexion

                   

                   

source:php.cn
Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Recommandations populaires
Tutoriels populaires
Plus>
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal
À propos de nous Clause de non-responsabilité Sitemap
Site Web PHP chinois:Formation PHP en ligne sur le bien-être public,Aidez les apprenants PHP à grandir rapidement!