普通网友 2011-06-24 08:57
浏览 3
已采纳

支持preg_replace检测语言标记并删除其他语言数据

I have a data string which contains 3 different language content enclosed in corresponding tags.

/[langStart-en] and //[langEnd-en] for English

/[langStart-ar] and //[langEnd-ar] for Arabic

/[langStart-fr] and //[langEnd-fr] for French

A language code will be passed as parameter through the url, say for eg. if the language is English, the url will be

article.php?lang = EN,

for Arabic article.php?lang = AR and

for French article.php?lang = FR.

I want to detect the Language and remove the contents for other languages from the string.

Hope this make sense

  • 写回答

2条回答 默认 最新

  • dongzi0602 2011-06-24 09:07
    关注

    I have a quick solution for you. Please check if it works.. You should be able to optimize the logic, but I hope this will help you to make it working.

    $pattern1 = '/\/\/\[langStart-en\][^n]*\/\/\[langEnd-en\]/';
    $pattern2 = '/\/\/\[langStart-ar\][^n]*\/\/\[langEnd-ar\]/';
    $pattern3 = '/\/\/\[langStart-fr\][^n]*\/\/\[langEnd-fr\]/';
    
    $lan= $_GET['lang'];
    $replace= '';
    $string = "whatever may be string";
    
    if ($lan=='EN')
    {
    $string = preg_replace($pattern2, $replace, $string);
    $string = preg_replace($pattern3, $replace, $string);
    
    }
    
    else if ($lan=='AR')
    {
    $string = preg_replace($pattern1, $replace, $string);
    $string = preg_replace($pattern3, $replace, $string);
    
    }
    
    else if ($lan=='FR')
    {
    $string = preg_replace($pattern1, $replace, $string);
    $string = preg_replace($pattern2, $replace, $string);
    }
    echo $string;
    

    Please check and let me know if you find any issues.

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

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料