0

困惑。我一直在使用下面的 IF PREG_MATCH 来区分整个单词和作为其他单词一部分的单词。它突然停止在这个脚本中运行,我使用的任何其他脚本都依赖于这个命令。

结果是它找到了部分单词,尽管您可以看到它被明确告知只查找整个单词。

$word = preg_replace("/[^a-zA-Z 0-9]+/", " ", $word);                                        

if (preg_match('#\b'.$word.'\b#',$goodfile) && (trim($word) != ""))  { 

        $fate = strpos($goodfile,$word);
        print $word ."  ";
        print $fate ."</br>";
4

2 回答 2

0

这会比爆炸更快更干净,而且你不会制作任何阵列

$first_word = stristr($lines, ' ', true); 
于 2011-04-16T01:35:51.797 回答
0

如果您只想阅读文本文件中一行的第一个单词,就像您的标题所暗示的那样,请尝试另一种方法:

// Get the file as an array, each element being a line
$lines = file("/path/to/file"); 

// Break up the first line by spaces
$words = explode(" ", $lines[0]); 

// Get the first word
$firstWord = $words[0]; 
于 2011-04-16T01:30:06.673 回答