开发者

PHP - RegEx problem

I have strings that are being read from a PHP file, for example:

<?php
// Dream Portal (c) 2009-2010 Dream Portal Team
// DreamPortal.english.php; @1.1

global $scripturl, $context;

// General Strings
$txt['forum'] = 'Forum';
$txt['dream_portal'] = 'Dream Portal';
$txt['dp_core_modules'] = 'Collapse or Expand this Module';
$txt['dp_who_forum'] = 'Viewing the forum index of <a href="' . $scripturl . '?action=forum">' . $context['forum_name'] . '</a>.';
$txt['dp_who_portal'] = 'Viewing the portal index of <a href="' . $scripturl . '">' . $context['forum_name'] . '</a>.';
$txt['dp_who_page'] = 'Viewing the page &quot;<a href="' . $scripturl . '?page=%1$s">%2$s</a>&开发者_开发技巧amp;quot;.';
?>

And here's the REGEX that I'm using currently that does what I need:

$data = file_get_contents('./'.$language.'/'.$file.'.'.$language.'.php');

$codes = array (
    '/(\' \. \$)(.+?)( \. \')/',
    '/(\= \$)(.+?)( \. \')/',
      '/(\' \. \$)(.+?)(\;)/',
    '/(\[\')(.+?)(\'\])/',
    '/<\?php/s', '/\?>/s', '/<\?/s'
);
$html = array (
    '{$2}',
    '= \'{$2}',
    '{$2}\';',
    '[$2]',
    '',
);
// Since we don't have the values for the vars.
$data = preg_replace($codes, $html, $data);

HOWEVER, since these strings are constantly changing, it may happen that a string like this is needed/inserted into any of the $txt array variables:

$txt['dp_who_page'] = 'Viewing the page &quot;<a href="'.$scripturl.'?page=%1$s">%2$s</a>&quot; . ';

Where the variable is bunched up with the . and the end of the string variable has this . '; which is different from what it was stated above, which was this:

$txt['dp_who_page'] = 'Viewing the page &quot;<a href="' . $scripturl . '?page=%1$s">%2$s</a>&quot;.';

In either of those cases, I need it to return the above $txt['dp_who_page'] like so:

Viewing the forum index of {context[forum_name]}.

Currently it doesn't work right with the 2nd case.

How can I do this using the following preg_replace code above for both cases listed above? What needs to be changed?

I am using the eval() php function to extract the variables.

OK, here is the entire function:

function loadLanguageFile($language, $file) {

    $temp = array();

    $data = file_get_contents('./'.$language.'/'.$file.'.'.$language.'.php');

    $codes = array (
        '/(\' \. \$)(.+?)( \. \')/',
        '/(\= \$)(.+?)( \. \')/',
          '/(\' \. \$)(.+?)(\;)/',
        '/(\[\')(.+?)(\'\])/',
        '/<\?php/s', '/\?>/s', '/<\?/s'
    );
    $html = array (
        '{$2}',
        '= \'{$2}',
        '{$2}\';',
        '[$2]',
        '',
    );
    // Since we don't have the values for the vars.
    $data = preg_replace($codes, $html, $data);

    // We must change this because they are global.
    $data = str_replace('$txt', '$langEditor_txt', $data);
    $data = str_replace('$helptxt', '$langEditor_helptxt', $data);

    eval($data);

    if (isset($langEditor_txt)) {
        $temp['txt'] = $langEditor_txt;
        unset($GLOBALS['langEditor_txt']);
    }
    if (isset($langEditor_helptxt)) {
        $temp['helptxt'] = $langEditor_helptxt;
        unset($GLOBALS['langEditor_helptxt']);
    }

    return $temp;
}

You can go here to see what I am trying to accomplish, basically, I want to allow strings to be translated into languages easier, based on the English strings of these languages: Language TEST using this function to load the language. Click on a language (currently only spanish, french, english available), than edit the language and you will see the preg_replace take affect. Also, if you edit the DreamPortal language, it is a good example of what it is supposed to do. Also, the Default text textarea boxes are (after you click on a language View link, than click on the Create It link) are loading up the english strings from the actual english files in all languages. So the preg_replace is using this to input that specific string definition into the Default text textarea.


I recommend to use gettext(). This way you can use gettext catalog files, which are editable with tools like Poedit (which uses translation memory as well).


I don't understand why you're doing this, but the immediate problem seems to be those here-and-gone-again spaces. The immediate solution would be to make the spaces optional:

'/(\'\s*\.\s*\$)(.+?)(\s*\.\s*\')/',
'/(=\s*\$)(.+?)(\s*\.\s*\')/',
'/(\'\s*\.\s*\$)(.+?)(;)/',
'/(\[\')(.+?)(\'\])/',
'/<\?php/s', '/\?>/s', '/<\?/s'

\s* matches zero or more whitespace characters.


If i were you i would use the native gettext features as already suggested or Zend_Translate or another i18n library.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜