2016-06-13 45 views
2

即时通讯尝试解码文本,这是我相信在WINDOWS-1251中提出的。 字符串看起来是这样的:没有分号编码

&#1040&#1075&#1077&#1085&#1090 

这应该代表了俄罗斯代理。这里的问题是:

  1. 我不能,除非我的每个数字
  2. 我不能做手工,因为我有一个10000行文本转换后加上分号这个字符串转换。

所以问题是,这是什么编码(无分号),我怎么能自动添加它们到每一行(正则表达式也许?),而不会破坏代码。

到目前为止,我一直在试图通过使用此代码来做到这一点:

应用逻辑

public function parseSentence((array) $sentences, $sentence, $i) { 
    if (strstr($sentence, '-')) { 
     $sentences[$i] = $this->explodeAndSplit('-', $sentence); 
    } else if (strstr($sentence, "'")) { 
     $sentences[$i] = $this->explodeAndSplit("'", $sentence); 
    } else if (strstr($sentence, "(")) { 
     $sentences[$i] = $this->explodeAndSplit("(", $sentence); 
    } else if (strstr($sentence, ")")) { 
     $sentences[$i] = $this->explodeAndSplit(")", $sentence); 
    } else { 
     if (strstr($sentence, '#')) { 
      $sentences[$i] = chunk_split($sentence, 6, ';'); 
    } 
    return $sentences; 
} 

/** 
* Explode and Split 
* @param string $explodeBy 
* @param string $string 
* 
* @return string 
*/ 
private function explodeAndSplit($explodeBy, $string) { 
    $exp = explode($explodeBy, $string); 
    for ($j = 0; $j < count($exp); $j++) { 
     $exp[$j] = chunk_split($exp[$j], 6, ';'); 
    } 
    return implode($explodeBy, $exp); 
} 

但很明显,这种做法是不正确一点(当然,完全不正确的) ,因为我没有考虑许多其他“特殊”角色。那么如何解决?

更新:
我使用流明的后端和AngularJS的前端。获取在Lumen(数据库/文本文件/ etc)中分析的所有数据,为AngularJS提供所谓的API路由来访问和检索数据。而事实是,在任何浏览器这个semicolonless编码工作巨大的,如果直接访问,但无法显示在角由于缺少分号

回答

3

这些都是Russian HTML Codes (Cyrillic)。为了确保它们正常显示,需要施加适当content-type

<meta http-equiv="content-type" content="text/html;charset=utf-8" /> 

我们正确地做到这一点,你要preg_split()的HTML代码上面的字符串你有,因此:

array_filter(preg_split("/[&#]+/", $str)); 

array_filter()只是删除任何空值。你也可以使用explode()来做同样的事情。


这将返回你有号的数组。从那里,一个简单的implode()所要求的前置&#和附加;很简单:

echo '&#' .implode(";&#", array_filter(preg_split("/[&#]+/", $str))) . ';'; 

将返回:当产生是正确的HTML

&#1040;&#1075;&#1077;&#1085;&#1090; 

现在,它显示以下俄文本:

Агент 

其中俄文翻译为Agent

+0

非常感谢这个惊人的演习,我的问题,简单的解决方案,真的很感谢 –

+0

@IvanZhivolupov我的荣幸!我很高兴它帮助你解决了你的问题! – Darren