2012-06-12 86 views
-1

我試圖從文件讀取此:正則表達式在PHP失敗

BEGIN_SEREFERRALS 2 google 13 15 search 2 2 END_SEREFERRALS 

我用這個函數:

 function getTextBetweenSEO($file) 
{ 
    $pattern = "/BEGIN_SEREFERRALS\s(.*)\sEND_SEREFERRALS/i"; 
    preg_match_all($pattern, $file, $matches); 
    print_r($matches); 
    exit(); 
    return $matches[1]; 

} 

我所印的是:

Array ([0] => Array () [1] => Array ()) 

爲什麼正則表達式失敗

+2

必須是別的東西,因爲你有什麼[只是作品(http://codepad.viper-7.com/eRUi2n) – PeeHaa

+0

這是對我很好! – undone

回答

0

y你可以試試這個正則表達式:

$ pattern =「^ BEGIN_SEREFERRALS(。*)END_SEREFERRALS ^」;

Array 
(
    [0] => Array 
     (
      [0] => BEGIN_SEREFERRALS 2 google 13 15 search 2 2 END_SEREFERRALS 
     ) 

    [1] => Array 
     (
      [0] => 2 google 13 15 search 2 2 
     ) 
) 

FIX有關blob文本數據模式:

$圖案= 「/BEGIN_SEREFERRALS(.*)END_SEREFERRALS/」;

getTextBetweenSEO("jsdfhksdjhskjfdsjfhsdkjfhBEGIN_SEREFERRALS 2 google 13 15 search 2 2 END_SEREFERRALSjklajdakldjaslkdjakldjalkdjalksdj") 

Array 
(
    [0] => Array 
     (
      [0] => BEGIN_SEREFERRALS 2 google 13 15 search 2 2 END_SEREFERRALS 
     ) 

    [1] => Array 
     (
      [0] => 2 google 13 15 search 2 2 
     ) 
) 
+0

沒有工作:(.............爲什麼我需要thouse ^^ ??!?字符串開始...是在文本..所以我不認爲^是相關 –

+0

當然你可以刪除^^格局是在全局文本數據。這一行很好:$ pattern =「/BEGIN_SEREFERRALS(.*)END_SEREFERRALS/」; – 0xBAADF00D

+0

我已編輯我的答案。 – 0xBAADF00D