Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have two bits of code that seem to be correct translations of one another. They unfortunately appear to return different values.

Code in Ruby:

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

Code in 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) shows an array of length 3, whereas textList.count => 4. What gives?

Anonymized $text example:

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: <CADPp44E47syuXvP1K-aemhcU7vdSijZkfKLu-74QPWs9U9551Q@mail.gmail.com>
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--

Run separate(text) on example or any gmail "view original" email in order to reproduce error

share|improve this question
    
Post both arrays result here... –  Henrique Barcelos May 20 '13 at 22:19
    
What is an example of $text? –  Explosion Pills May 20 '13 at 22:20
    
@HenriqueBarcelos In short, the 3rd array element in the PHP array is two array elements (3 and 4) in the ruby array even though they are both split by a regex split method. –  Pinwheeler May 20 '13 at 22:35
    
@ExplosionPills $text example posted –  Pinwheeler May 20 '13 at 22:35
    
If you want quicker help I'd suggest to remove the code and input which is not relevant and present the least needed reproduce the problem. –  Qtax May 20 '13 at 23:14

1 Answer 1

up vote 0 down vote accepted

BINGO ZINGO Figured it out!

Apparently, in PHP, in order to change a variable inside a loop involving that variable, you have to preface the variable with '&'

Added '&' and fixed some general recursion errors and it ran smoothly.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.