如何使用PHP从web中删除印地文文本

时间:2015-07-25 15:47:39

标签: php unicode page-curl

在这里,我试图从网上(在网址中)废弃数据,但是我得到了这样的回应

  

\ u093f \ u0938 \

如何解码这个unicode?请建议我用PHP做什么脚本。

此脚本与英文文本正常工作,所以英语发生了什么。我已经使用此脚本删除了数据。我知道这个响应是dev nagri unicode但是如何解码它。

我提前感谢php问题的新手

svg.selectAll("g.y-axis").call(yAxis); //<-- dash not underscore
svg.selectAll("g.x-axis").call(xAxis);

3 个答案:

答案 0 :(得分:1)

如果您运行的是PHP 5.4或更高版本,请在调用json_encode时传递JSON_UNESCAPED_UNICODE参数。

$i= 1;
for($i; $i < 6; $i++)
{
    $html file_get_contents("http://www.jagran.com/jokes/child/jokes-1262211".$i.".html");
    libxml_use_internal_errors(true);
    $dom = new DOMDocument();
    $dom->loadHTML($html);
    libxml_clear_errors();
    $nodes = $dom->getElementsByTagName('p');
    $item = array();
    $articles = array();
    foreach ($nodes as $node) {
         $item['msg'] = (strlen($node->nodeValue) > 20 ? $node->nodeValue : '');
         $item['cat_id'] = 1;
         if($item['msg'] !="")
         $articles[] = array_unique($item);
    }
    $articles = json_encode($articles, JSON_UNESCAPED_UNICODE);
//--------------------add-this---------------------^
    print_r($articles);
}

答案 1 :(得分:0)

你非常接近。你收到了标志:Ç和स

首先你可以试试谷歌的角色,你会发现字符的devnagari含义:

https://www.google.de/#q=%5Cu093f

https://www.google.de/#q=%5Cu0938

如果要在html中显示unicode,则必须将编码从/ u0123更改为&amp;#x123。见这里:

db.collection.aggregate([
  { "$group": {
    "_id": {
      "$size": {
        "$filter": {
          "input": { "$map": {
            "input": ["A","B","C","D"],
            "as": "el",
            "in": { "$cond": [
              { "$eq": [ "$$el", "A" ] },
              "$workHome",
              { "$cond": [
                { "$eq": [ "$$el", "B" ] },
                "$commute",
                { "$cond": [
                  { "$eq": [ "$$el", "C" ] },
                  "$tel",
                  "$weekend"
                ]}
              ]}
            ]}
          }},
          "as": "el",
          "cond": {
            "$eq": [ "$$el", true ]
          }
        }
      }
    },
    "count": { "$sum": 1 }
  }},  
  { "$sort": { "_id": 1 } }
])

但是,当你想要去印度语时,你应该开始学习如何阅读和处理unicode。接下来的问题是,您希望如何处理结果。

答案 2 :(得分:0)

我认为 PHPhil 的答案很好,我赞成它。我编辑了代码,因为它不能仅仅执行php部分 - 而是重要的是添加正确的元标记(请参阅下面的代码)以正确显示devnagari。我也想用缺失的“=”来纠正错误。不幸的是我的编辑被拒绝了所以我必须添加一个新的答案和代码更正。

<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<?php

$i= 1;
for($i; $i < 6; $i++)
{
    $html = file_get_contents("http://www.jagran.com/jokes/child/jokes-1262211".$i.".html");
    libxml_use_internal_errors(true);
    $dom = new DOMDocument();
    $dom->loadHTML($html);
    libxml_clear_errors();
    $nodes = $dom->getElementsByTagName('p');
    $item = array();
    $articles = array();
    foreach ($nodes as $node) {
         $item['msg'] = (strlen($node->nodeValue) > 20 ? $node->nodeValue : '');
         $item['cat_id'] = 1;
         if($item['msg'] !="")
         $articles[] = array_unique($item);
    }
    $articles = json_encode($articles, JSON_UNESCAPED_UNICODE);
//--------------------add-this---------------------^
    print_r($articles);
}
?>
</body>
</html>
相关问题