doujie7886 2011-03-29 21:47
浏览 40
已采纳

在单行上删除特殊字符█之后的字符串或单词

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 help 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("
", 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++;
        }
      }
    }
  • 写回答

2条回答 默认 最新

  • doufang2228 2011-03-29 22:05
    关注

    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 != '').

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误