Page 1 of 1

Matching through define() blocks

Posted: Tue Nov 27, 2007 7:09 am
by shiznatix
EDIT: for some reason its doubling the spaces on the first block of code i have here. Ignore the double spaced stuff.

I have php files that hold my translations and I am working on a script that while viewing a page, it gives me a link that says "translate this page" and then it lets you translate it. Fun, cool, awesome, I know!

The problem I am having is I can't get this regex down to help me extract the phrases and their keys. An example file that I am having problem with looks exactally like this:

Code: Select all

 
<?php
define('USERS_RAKEBACK_CALCULATOR_TITLE', 'Poker Rakeback Calculator - Use rakeback calculator to estimate your rake and rakeback');
define('USERS_RAKEBACK_CALCULATOR_NAV_CALCULATE_RAKEBACK', 'Calculate Rakeback');
 
define('USERS_RAKEBACK_CALCULATOR_POKER_RAKEBACK_CALCULATOR', 'Poker Rakeback Calculator');
define('USERS_RAKEBACK_CALCULATOR_BLURB_PARAGRAPH_1',         'To get an estimate of how much rake you are paying and your expected rakeback, enter your game type and time played per week in the rakeback calculator.');
define('USERS_RAKEBACK_CALCULATOR_GAME_TYPE',                 'Game Type:');
define('USERS_RAKEBACK_CALCULATOR_TABLES_SIMULTANEOUSLY',     'Tables simultaneously:');
define('USERS_RAKEBACK_CALCULATOR_1_TABLE',                   '1 table');
define('USERS_RAKEBACK_CALCULATOR_2_TABLES',                  '2 tables');
define('USERS_RAKEBACK_CALCULATOR_3_TABLES',                  '3 tables');
define('USERS_RAKEBACK_CALCULATOR_4_TABLES',                  '4 tables');
define('USERS_RAKEBACK_CALCULATOR_5_TABLES',                  '5 tables');
define('USERS_RAKEBACK_CALCULATOR_6_TABLES',                  '6 tables');
 
define('USERS_RAKEBACK_CALCULATOR_TIME_PLAYED_PER_WEEK',      'Time played per week:');
define('USERS_RAKEBACK_CALCULATOR_4_HOURS_CASUAL',            '4hrs - Casual');
define('USERS_RAKEBACK_CALCULATOR_8_HOURS_WEEKENDER',         '8hrs - Weekender');
define('USERS_RAKEBACK_CALCULATOR_12_HOURS_HOBBYIST',         '12hrs - Hobbyist');
define('USERS_RAKEBACK_CALCULATOR_20_HOURS_AMATEUR',          '20hrs - Amateur');
define('USERS_RAKEBACK_CALCULATOR_30_HOURS_SEMI_PRO',         '30hrs - Semi Pro');
define('USERS_RAKEBACK_CALCULATOR_40_HOURS_PROFESSIONAL',     '40hrs - Professional');
define('USERS_RAKEBACK_CALCULATOR_60_HOURS_POKER_FREAK',      '60hrs - Poker Freak');
define('USERS_RAKEBACK_CALCULATOR_RAKEBACK',                  'Rakeback:');
 
define('USERS_RAKEBACK_CALCULATOR_DAILY_RAKE_AND_RAKEBACK',   'Daily rake $%1$s and rakeback <b>$%2$s</b>');
define('USERS_RAKEBACK_CALCULATOR_WEEKLY_RAKE_AND_RAKEBACK',  'Weekly rake $%1$s and rakeback <b>$%2$s</b>');
define('USERS_RAKEBACK_CALCULATOR_MONTHLY_RAKE_AND_RAKEBACK', 'Monthly rake $%1$s and rakeback <b>$%2$s</b>');
?>
 
and what I am doing is this:

Code: Select all

 
$contents = file_get_contents($file_path);
 
preg_match_all("#define\(\'([A-Z_]+)\', \'(.*)\'\);#", $contents, $matches);
 
but if I print_r($matches) it only gives me these ones:

Code: Select all

 
Array
(
    [0] => Array
        (
            [0] => define('USERS_RAKEBACK_CALCULATOR_TITLE', 'Poker Rakeback Calculator - Use rakeback calculator to estimate your rake and rakeback');
            [1] => define('USERS_RAKEBACK_CALCULATOR_NAV_CALCULATE_RAKEBACK', 'Calculate Rakeback');
            [2] => define('USERS_RAKEBACK_CALCULATOR_POKER_RAKEBACK_CALCULATOR', 'Poker Rakeback Calculator');
            [3] => define('USERS_RAKEBACK_CALCULATOR_MONTHLY_RAKE_AND_RAKEBACK', 'Monthly rake $%1$s and rakeback $%2$s');
        )
 
    [1] => Array
        (
            [0] => USERS_RAKEBACK_CALCULATOR_TITLE
            [1] => USERS_RAKEBACK_CALCULATOR_NAV_CALCULATE_RAKEBACK
            [2] => USERS_RAKEBACK_CALCULATOR_POKER_RAKEBACK_CALCULATOR
            [3] => USERS_RAKEBACK_CALCULATOR_MONTHLY_RAKE_AND_RAKEBACK
        )
 
    [2] => Array
        (
            [0] => Poker Rakeback Calculator - Use rakeback calculator to estimate your rake and rakeback
            [1] => Calculate Rakeback
            [2] => Poker Rakeback Calculator
            [3] => Monthly rake $%1$s and rakeback $%2$s
        )
 
)
 
So obviously the regex I am using is not good but I don't see the problem. Can someone shed some light on this one?

Posted: Tue Nov 27, 2007 7:20 am
by aaronhall
Whitespace problem between the two arguments.. try this:

Code: Select all

preg_match_all("#define\(\'([A-Z_]+)\',[ \t\n\r]*\'(.*)\'\);#", $contents, $matches);

Posted: Tue Nov 27, 2007 12:31 pm
by feyd
Wouldn't using get_defined_constants() work better?

Posted: Wed Nov 28, 2007 3:39 am
by shiznatix
feyd wrote:Wouldn't using get_defined_constants() work better?
negative because I am running a cron job that is makeing sure my language files are up to date in comparison to my english translation so the constants are not defined. Plus I have a bunch of other constants that I don't want to be put into this. This is the best way and its easy.

Re: Matching through define() blocks

Posted: Tue Jan 15, 2008 2:44 am
by shiznatix
Everything was working properly until I noticed one bad thing going on. If I have something like this:

Code: Select all

 
define('USERS_INDEX_TITLE', '
    Rakeback.com The Poker Loyalty program
');
 
It is not being picked up by this regex that I am currently using:

Code: Select all

 
preg_match_all("#define\(\'([A-Z_0-9]+)\',[ \t\n\r]*\'(.*)\'\);#", $fileContents, $matches);
 
Of course it has to be somewhere in the (.*) part but I don't know how to add in a "maybe there will be line breaks between the 2 single quotes" statment into that. If someone could help me that would be great.

Re: Matching through define() blocks

Posted: Tue Jan 15, 2008 3:06 am
by VladSun

Code: Select all

 
$s =  "
    define('USERS_INDEX_TITLE', '
     Rakeback.com The Poker Loyalty program
 ');
 
    define('USERS_INDEX_VALUE', '
     New.com 
 ');
 
    define('USERS_INDEX_VALUE', 'New2.com');
 
 ";
  
  
preg_match_all("/define\('([A-Z_0-9]+)'\s*,\s*'(.*?)\s*'\);/is", $s, $matches);
 
print_r($matches);
 
Again? ;)
viewtopic.php?f=38&t=74793 :P

Re: Matching through define() blocks

Posted: Tue Jan 15, 2008 4:05 am
by shiznatix
haha yes i forgot about that one. I have a lot of trouble reading regex and get easily lost but your solution works the perfect. thanks.