How to check if a string contain any text

This question already has an answer here:

  • How do I check if a string contains a specific word? 37 answers

  • Use the strpos function: http://php.net/manual/en/function.strpos.php

    $haystack = "foo bar baz";
    $needle   = "bar";
    
    if( strpos( $haystack, $needle ) !== false ) {
        echo ""bar" exists in the haystack variable";
    }
    

    In your case:

    if( strpos( $a, 'some text' ) !== false ) echo 'text';
    

    Note that my use of the !== operator (instead of != false or == true or even just if( strpos( ... ) ) { ) is because of the "truthy"/"falsy" nature of PHP's handling of the return value of strpos .


    Empty strings are falsey, so you can just write:

    if ($a) {
        echo 'text';
    }
    

    Although if you're asking if a particular substring exists in that string, you can use strpos() to do that:

    if (strpos($a, 'some text') !== false) {
        echo 'text';
    }
    

    http://php.net/manual/en/function.strpos.php我认为你是wondiner如果'一些文本'存在于字符串中吗?

    if(strpos( $a , 'some text' ) !== false)
    
    链接地址: http://www.djcxy.com/p/74086.html

    上一篇: 让javascript搜索innerHTML?

    下一篇: 如何检查一个字符串是否包含任何文本