如何判断字符串是否包含某些字符$str="modcss/img/h3_bg/h3_14_bg.png"判断$str里面是否包含 "h3_14_g.png" 这段字符串用什么函数?
是不是必须用preg_match? preg_match好像只能正则匹配吧。

解决方案 »

  1.   

    <?php 
    $str="modcss/img/h3_bg/h3_14_bg.png" ;
    if (strstr($str,"h3_14_bg.png")!=="false"){
        echo strstr($str,"h3_14_bg.png");
    }
    ?>
      

  2.   

    楼上正解!$str="modcss/img/h3_bg/h32_14_bg.png";
    if(strpos($str,'h3_14_bg.png'))
    {
    echo "exists!";
    }else
    {
    echo "not exists!";
    }
      

  3.   

    另外的思路
    $str="modcss/img/h3_bg/h3_14_bg.png";
    echo (str_ireplace('h3_14_bg.png','',$str) != $str) ? 'exists!' : 'not exists!';
      

  4.   

    strpos要比strstr节省内存. 在php手册里面这样写的:
    注: If you only want to determine if a particular needle occurs within haystack, use the faster and less memory intensive function strpos() instead. 
      

  5.   

    $str="modcss/img/h3_bg/h3_14_bg.png" ;
    $pos1 = strpos($str,'h3_14_bg.png');
      

  6.   

    The 7 floor is good answer.