tags:

views:

87

answers:

3

Hi,

Can somebody tell me what I am doing wrong really? I am going nuts, the following code works perfect on localhost/WIN and when I try it on the webhost/linux it gives warnings:

$lines = file('english.php');
foreach($lines as $line) {
    $matches=array();
    if (preg_match('/DEFINE\(\'(.*?)\',\s*\'(.*)\'\);/i', $line, $matches)) {
        $keys[] = $matches[1];
        $values[] = $matches[2];
    }
}
$lang = array_combine($keys, $values);

When I test on webhost:

Warning: array_combine() expects parameter 1 to be array, null given in /home/xx/public_html/xx on line 616

But on local server (windows xp) it works perfect. I do not know what I am doing wrong, please help me resolve this nightmare :(

Thanks.

A: 

Are the php versions the same?

And are you sure you have transfered all your files to the webhost?

Gamecat
array_combine() is only available in PHP 5. If the server had PHP 4, a "function does not exist" error would be shown instead of "array_combine() expects parameter 1 to be array"
Seb
The code is a clone copy. The php version is 5.2.8 on both even..
Ahmad Fouad
A: 

It seems your $keys variable is null, because you're not initializing it anywhere.

My best guess is that the english.php file on your server is empty (or does not exists), so when you try to read it nothing is saved in $keys variable;

Try adding an initial value for that variable before the foreach statement:

$lines = file('english.php');
$keys = array();
foreach($lines as $line) {
$matches=array();
    if (preg_match('/DEFINE\(\'(.*?)\',\s*\'(.*)\'\);/i', $line, $matches)) {
        $keys[] = $matches[1];
        $values[] = $matches[2];
    }
}
$lang = array_combine($keys, $values);

That way, even if the file doesn't exist or is empty you're covering all possible paths.

You should always code as if everything could go wrong, not the other way around :)

Seb
Thanks. The file exists and contains 89 lines of data, also when I test on localhost the results are printed to the browser.. that's why I am confused. because it works without problem on localhost, but not on the webhost.. :)
Ahmad Fouad
Update: I added $keys = array(); and $values = array(); below $lines and it still works on localhost perfectly (it prints the results) but on web host it changed the error: Warning: array_combine() [function.array-combine]: Both parameters should have at least 1 element in /home/...
Ahmad Fouad
It says they are empty arrays, but thats not true, the file contains 89 lines and localhost can read them all.. confused :)
Ahmad Fouad
Mmm.. tricky one. english.php file is exactly the same on localhost vs. server? Try making a var_dump($lines) just after you open the file, both locally and your server. Maybe some weird path setting is making you believe you're getting the same file when you're not.
Seb
+2  A: 

I don't see anything obviously wrong with your code, but I'm curious why you're building separate arrays and then combining them rather than just building a combined array:

// Make sure this file is local to the system the script is running on.
// If it's a "url://" path, you can run into url_fopen problems.
$lines = file('english.php');

// No need to reinitialize each time.
$matches = array();

$lang = array();
foreach($lines as $line) {
    if (preg_match('/DEFINE\(\'([^\']*)\',\s*\'([^\\\\\']*(?:\\.[^\\\\\']*)*)\'\);/i', $line, $matches)) {
        $lang[$matches[1]] = $matches[2];
    }
}

(I've also changed your regex to handle single quotes.)

Ben Blank
You're boss! The problem was with the file itself..and i corrected my code per your instructions too
Ahmad Fouad