开发者

Removing what ever string of word or words come after a special char █ on a single line

I have a source file that file that contain to be translated strings. Each line of the translation cache files containt a Source string then a delimiter/separatpr then the translated string

The original old author did write a nice clean up script that cleans the cache files from duplicates or deletes entire lines from the cache if the old source did not use them anymore.

Now I would like to add one more function into that existing code (relevant part below) which is : to delete the entire line when there is absolutely nothing '' coming after the separation key for the same line that its examining.

I have the feeling it will be 1 line of code somewhere below. But where how does this line look like? Your h开发者_开发知识库elp is much appreciated. Thanks very much in advance.

CURRENT CACHE FILE:

Bla blaaaa bla blaa blaaaaa bla█Bla blaaaa bla blaa blaaaaa bla
Zorro in zeppelin from zimbabwe█
Bla blaaaa bla blaa blaaaaa bla█Bla blaaaa bla blaa blaaaaa bla
Water wave with whistling whale█

MY GOAL CACHE FILE:

Bla blaaaa bla blaa blaaaaa bla█Bla blaaaa bla blaa blaaaaa bla
Bla blaaaa bla blaa blaaaaa bla█Bla blaaaa bla blaa blaaaaa bla

EXISTING CODE:

$translation_key_value_separator = '█';

// Read file contents and create a used keys array
$used_keys = array();
get_used_keys($dir_to_clean, &$used_keys);

// Cleanup translations: removes an entire line from the translation cache file 
// if the LEFT part of the █ is not existend (anymore) in the source files.
$removed = array();
$counter = 0;
foreach (glob($dir_translation_files . "/*.$translation_ext") as $filepath) {
    $filename = get_filename($filepath);
    $key_values = explode("\n", file_get_contents($filepath));
    $clean_file = array();
    foreach($key_values as $key_value){
      list($key, $value) = explode($translation_key_value_separator, $key_value);
      if($key != ''){
        if(in_array($key, $used_keys)){
            $clean_file[] = $key . $translation_key_value_separator . $value;
        }else{
            $removed[$filename][] = $key;
            $counter++;
        }
      }
    }


It looks like you need to add another conditional when checking whether to add the line to $clean_file or $removed. Please see the portion I extracted below:

if ($key != '') {
    if (in_array($key, $used_keys) && ($value != '')) {
        $clean_file[] = $key . $translation_key_value_separator . $value;
    } else {
        $removed[$filename][] = $key;
        $counter++;
    }
}

where the extra condition is ($value != '').


$cache = "Bla blaaaa bla blaa blaaaaa bla█Bla blaaaa bla blaa blaaaaa bla
Zorro in zeppelin from zimbabwe█
Bla blaaaa bla blaa blaaaaa bla█Bla blaaaa bla blaa blaaaaa bla
Water wave with whistling whale█"; 

$lines = explode("\n", $cache);
foreach($lines as $line){
    $x = explode("█", $line);
    if(!empty($x[1])) $new[] = $line;
}

print_r($new);

Result:

Array
(
    [0] => Bla blaaaa bla blaa blaaaaa bla█Bla blaaaa bla blaa blaaaaa bla
    [1] => Bla blaaaa bla blaa blaaaaa bla█Bla blaaaa bla blaa blaaaaa bla
)

You can $new = implode("\n", $new); to get string instead of array.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜