2013-05-20 55 views
-1

我有兩段代碼似乎是彼此正確的翻譯。不幸的是他們似乎返回不同的值。從Ruby到PHP移植時的正則表達式問題

代碼在紅寶石:

def separate(text,boundary = nil) 
    # returns array of strings and arrays containing all of the parts of the email 
    textList = [] 
    if !boundary #look in the email for "boundary= X" 
     text.scan(/(?<=boundary=).*/) do |bound| 
      textList = recursiveSplit(text,bound) 
      end 
    end 
    if boundary 
     textList = recursiveSplit(text,boundary) 
    end 
    puts textList.count 
    return textList 
end 


def recursiveSplit(chunk,boundary) 
    if chunk.is_a? String 
     searchString = "--" + boundary 
     ar = chunk.split(searchString) 
     return ar 
    elsif chunk.is_a? Array 
     chunk do |bit| 
      recursiveSplit(bit,boundary); 
     end 
    end 
end 

代碼在PHP:

function separate($text, $boundary="none"){ 
    #returns array of strings and arrays containing all the parts of the email 
    $textBlock = []; 
    if ($boundary == "none") { 
     preg_match_all('/(?<=boundary=).*/', $text, $matches); 
     $matches = $matches[0]; 
     foreach ($matches as $match) { 
      $textList = recursiveSplit($text,$match); 
     } 
    }else { 
     $textList = recursiveSplit(text,boundary); 
    } 
    var_dump($textList); 
    return$textList; 
} 

function recursiveSplit($chunk,$boundary){ 
    if (is_string($chunk)) { 
     $ar = preg_split("/--".$boundary."/", $chunk); 
     //$ar = explode($searchString, $chunk); 
     return $ar; 
    } 
    elseif (is_array($chunk)) { 
     foreach ($chunk as $bit) { 
      recursiveSplit($bit,$boundary); 
     } 
    } 
} 

var_dump($textList)示出了長度爲3的陣列,而textList.count => 4是爲什麼呢?

匿名的$文本例如:

MIME-Version: 1.0 
Received: by 10.112.170.40 with HTTP; Fri, 3 May 2013 05:08:21 -0700 (PDT) 
Date: Fri, 3 May 2013 08:08:21 -0400 
Delivered-To: [email protected] 
Message-ID: <[email protected]om> 
Subject: MiB 5/3/13 7:43AM (EST) 
From: Me <[email protected]> 
To: Someone <[email protected]> 
Content-Type: multipart/mixed; boundary=BNDRY1 

--BNDRY1 
Content-Type: multipart/alternative; boundary=BNDRY2 

--BNDRY2 
Content-Type: text/plain; charset=ISO-8859-1 

-TEXT STUFF HERE. SAYING THINGS 
ABOUT CERTAIN THINGS 

--BNDRY2 
Content-Type: text/html; charset=ISO-8859-1 

<div dir="ltr">-changed signature methods to conform more to working clinic header methods(please test/not testable in simulator)<div style>-confirmed that signature image is showing up in simulator. Awaiting further tests</div> 
<div style>-Modified findings spacing/buffer. See if you like it</div></div> 

--BNDRY2-- 
--BNDRY1 
Content-Type: application/zip; name="Make it Brief.ipa.zip" 
Content-Disposition: attachment; filename="Make it Brief.ipa.zip" 
Content-Transfer-Encoding: base64 
X-Attachment-Id: f_hg9biuno0 

<<FILE DATA>> 
--BNDRY1-- 

運行separate(text)的例子或任何Gmail才能重現 「查看原圖」 電子郵件錯誤

+0

在這裏發佈兩個數組結果... –

+0

'$ text'的例子是什麼? –

+0

@HenriqueBarcelos簡而言之,PHP數組中的第3個數組元素是ruby數組中的兩個數組元素(3和4),儘管它們都是通過正則表達式拆分方法分割的。 – Pinwheeler

回答

0

BINGO驚鼓想通了!

顯然,在PHP中,爲了改變一個循環內的變量包括變量,你有「&」

新增「&」,以前言可變和固定的一般遞歸錯誤,它運行平穩。