2011-04-16 25 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);