简体   繁体   English

用PHP修复文本文件中的怪异缩进

[英]Fixing weird indentation in text file with php

I'm taking this file and splitting it up into sentences. 我正在获取此文件并将其拆分为句子。 The issue is that its formatted weirdly. 问题在于其格式怪异。 I need to remove all the random new lines, indentations and unneeded spaces. 我需要删除所有随机的新行,缩进和不需要的空格。 Is there a way to do this with php? 有没有办法用PHP做到这一点?

I am currently using 我目前正在使用

$test= file_get_contents("text.txt");
$stringtest = str_replace(PHP_EOL,'', $test);

But I am getting weird behavior when I try to split up the sentences. 但是当我尝试拆分句子时,我的行为变得很奇怪。 Is there a way to do this? 有没有办法做到这一点?

The weird behavior is that when I print out the text 奇怪的行为是当我打印出文本时

echo $stringtest;

There are unseen characters between lines where a newline/weird_spacing used to exist. 以前存在换行符/ weird_spacing的行之间有看不见的字符。

You can use a regex to merge all whitespaces to a single space. 您可以使用正则表达式将所有空格合并为单个空格。 Also you probably want to remove whitespace at the beginning and end. 另外,您可能想删除开头和结尾的空格。 Try this: 尝试这个:

$test = trim($test);
$test = preg_replace('/\s+/s', ' ', $test);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM