【问题标题】:PHP, explode a text file that have special characters between every sentencePHP,爆炸每个句子之间都有特殊字符的文本文件
【发布时间】:2017-04-03 12:20:26
【问题描述】:

我有一个文本文件使用 Unicode 编码(有时它包含希伯来语),其中包含文本,并且在每个句子之间有两个特殊字符 (■■) 像这样 (alt + 2 + 5 + 4 ) 这是一个示例

"How are you, It's a cool day .. how can I read this sentence in a string variable with the quotes."■■

"This is second sentence I want it in another variable or in second cell in array."■■


This is third sentence. It is without quotes I want to read it to a string.■■

This is fourth sentence it includes an enter,
The fourth sentence isn't completed right yet.

The sentence doesn't completed until the special character is appears,


Maybe there a lot of lines in the same sentence.
This is the last part of the sentence■■

"This is the fifth sentence", this is the second part of the fifth sentence■■

Maybe between every two sentences there is a lot of empty lines, This is the sixth sentence■■



Thanks I wish anyone can help me with this, This is the last sentence■■

我纠正了这段代码,但它不起作用:

<?php
$file=file_get_contents("text_file.txt");
$file_= urldecode($file);
$sentences = explode('■■',$file_);
?>

当我为第一句话制作指定代码时(我没有把所有文件都放好以便阅读)我得到

Array ( [0] => ��"How are you, It s a cool day .. how can I read this sentence in a string variable with the quotes."�%�%

其他句子也一样..(我看到所有句子都在同一行)我的意思是没有输入

另一件事我想忽略两个句子之间的行我的意思是忽略■■之后的空行

【问题讨论】:

  • 适用于所有 PHP 从 5.4 到 7.1
  • 当我从问题中复制您的特殊字符时,我得到 Hex A6 而不是您建议的 Hex FE,您确定您在 explode() 中使用了正确的分隔符吗??跨度>
  • urldecode 在里面做什么?它看起来不像是 URL 编码的数据。
  • 当我第一次看到这个问题时,我很确定 url_decode() 最初并不存在。请注释您可能做出的任何修改,因为更改问题使回答变得更加困难
  • 很遗憾,我们只能猜测,除非您绝对确定您使用的分隔符是文件中的分隔符。知道这个字符是简单的单字节字符集还是多字节字符集也很有用?

标签: php string text-files special-characters explode


【解决方案1】:

使用mb_split 代替explode

【讨论】:

  • 当然只是评论,而不是答案
【解决方案2】:

尝试使用:■或 & #x25A0;而不是■。

【讨论】:

    【解决方案3】:

    我找到了解决方案... 我必须将文本文件保存为 UTF-8 而不是 Unicode。 现在它可以工作了 谢谢

    【讨论】:

      【解决方案4】:

      另一种方法(因为它也不适合我):

      <?php
      
      $result = array();
      $file = explode("■■", file_get_contents("sentences.txt")); /* EDIT : 2 x ALT254*/
      foreach ( $file as $content ) {
      $result[] = $content;
      }
      print_r($result);
      
      ?>
      

      【讨论】:

      • 这将实现什么?
      • 和他想要的一样(对我来说,他的代码没问题,但我什么也没得到)这就是为什么我试图以不同的方式得到一些东西
      • @RiggsFolly :无论如何,它不值得 -1 恕我直言,因为我提到这是“另一种方法(因为它也不适合我)”
      • 就像我说的,投反对票的不是我。我认为您的代码完全符合 OP 的功能,但我同意它不值得投反对票
      • 明显一样!我试图弄清楚为什么他的代码不能正常工作(顺便说一句:我用 ALT254 得到了正确的结果)
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-13
      • 2015-08-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多