2011-04-16 49 views
0

混淆。我一直在使用下面的IF PREG_MATCH来区分单词和单词,这些单词和单词是其他单词的一部分。它在此脚本中突然停止运行,并且我使用的任何其他脚本都依赖于此命令。preg_replace突然停止作出区别

结果是找到了单词的部分,尽管你可以看到它被明确地告诉找到整个单词。

$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>"; 

回答

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]; 
+0

啊别提我很困惑 - 对strpos功能不符合的preg_match同步,它只是寻找文件中的字符串的第一个实例,而的preg_match是找到实际整个w ORD – Graham 2011-04-16 01:41:06

0

这将是更快,更清洁比爆炸,你不会作出任何阵列

$first_word = stristr($lines, ' ', true);