Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/234.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 编码中没有分号_Php_Character Encoding - Fatal编程技术网

Php 编码中没有分号

Php 编码中没有分号,php,character-encoding,Php,Character Encoding,我正在尝试解码WINDOWS-1251中显示的文本。 字符串如下所示: &#1040&#1075&#1077&#1085&#1090 在俄语中代表代理。问题是: 除非在每个数字后添加分号,否则无法转换此字符串 我不能手动操作,因为我有10000行文字需要转换 所以问题是,这种编码是什么(没有分号),我怎样才能在不破坏代码的情况下将它们自动添加到每一行(可能是regex?) 到目前为止,我一直在尝试使用以下代码: 应用程序逻辑 public functio

我正在尝试解码WINDOWS-1251中显示的文本。 字符串如下所示:

&#1040&#1075&#1077&#1085&#1090
在俄语中代表代理。问题是:

  • 除非在每个数字后添加分号,否则无法转换此字符串
  • 我不能手动操作,因为我有10000行文字需要转换
  • 所以问题是,这种编码是什么(没有分号),我怎样才能在不破坏代码的情况下将它们自动添加到每一行(可能是regex?)

    到目前为止,我一直在尝试使用以下代码:

    应用程序逻辑

    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);
    }
    
    公共函数语法语句((数组)$句子,$句子,$i){
    如果(strstr($句子,'-')){
    $SECTIONS[$i]=$this->explodesandsplit('-',$SECTION);
    }else if(strstr($句,“')){
    $SECTIONS[$i]=$this->explodesandsplit(“'”,$SECTION);
    }else if(strstr($句,“(”)){
    $SECTIONS[$i]=$this->explodesandsplit(“(”,$SECTION);
    }else if(strstr($句,“)”){
    $SECTIONS[$i]=$this->explodesandsplit(“)”,$SECTION);
    }否则{
    if(strstr($句子“#”)){
    $SECTIONS[$i]=块分割($SECTION,6,“;”);
    }
    返回$SECTIONS;
    }
    /**
    *炸裂
    *@param string$explodeBy
    *@param string$string
    *
    *@返回字符串
    */
    私有函数explodeAndSplit($explodeBy,$string){
    $exp=explode($explodeBy,$string);
    对于($j=0;$j
    但很明显,这种方法有点不正确(完全不正确),因为我没有考虑到许多其他的“特殊”字符。那么如何修复它呢

    更新:
    我使用Lumen作为后端,AngularJS作为前端。在Lumen中解析所有数据(数据库/文本文件/等等),为AngularJS提供所谓的API路由来访问和检索数据。问题是,如果直接访问,这种无分号编码在任何浏览器中都非常有效,但由于缺少分号,因此无法以角度显示。为了确保正确显示它们,您需要一个适当的
    内容类型
    应用程序d:

    <meta http-equiv="content-type" content="text/html;charset=utf-8" />
    
    简单地删除任何空值。您也可以使用它来做同样的事情


    这将返回您拥有的一个数字数组。从这里开始,一个简单的
    内爆()
    ,带有所需的前缀
    &
    和附加
    ,非常简单:

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

    &#1040;&#1075;&#1077;&#1085;&#1090;
    
    现在,当生成正确的HTML时,它将显示以下俄语文本:

    Агент
    

    这直接翻译成俄语的
    Agent

    非常感谢您用我的问题,简单的解决方案,真的appreciate@IvanZhivolupov我的荣幸!我很高兴它帮助你解决了你的问题!
    Агент