在字符串中查找单词

时间:2013-10-23 14:41:27

标签: php string file search strstr

很简单,我有一个文件,每行都有类似的东西

OneTwoThreeFourFiveSixSeven OneTwoThreeFourFiveSixSeven OneThreeFourSevenFiveSix
OneThreeFourSevenFiveSix OneTwoThreeFourFiveSixSeven OneThreeFourSevenFiveSix

如何检查这两行中是否包含单词“Five”?

我试过了:

strstr();
strrpos();
strpos();

和其他人一样,但没有什么能给我提供我正在寻找的功能。 在PHP中是否有一些构建方法,或者我是否必须尝试构建将要查找的函数?

好的,这就是代码:

foreach ($results as $result) {
    foreach ($lines as $line) {
        if(strpos($line, '$this->' . $result) || strpos($line, '($this->' . $result)){
            $i++;
        }
    }

    if($i == 0 && $result !== "__construct"){
        print_r($result);
    }
}

1 个答案:

答案 0 :(得分:0)

试试这段代码:

  

strrpos - 在a中查找最后一个子字符串的位置   串

     

如果未找到针,则返回FALSE。

<?php
    $str1 = "OneTwoThreeFourFiveSixSeven";
    $str2 = "OneThreeFourSevenFiveSix";
    if(strrpos($str1, "Two") !== FALSE){
        echo 'Two is in $str1';
    }else{
        echo 'Two is not in $str1';
    }

    if(strrpos($str2, "Two") !== FALSE){
        echo 'Two is in $str2';
    }else{
        echo 'Two is not in $str2';
    }
?>

在键盘中测试:http://codepad.org/8KbwMq36

相关问题