我有如下文本文件(注意开头的空格):
aaaaaaaaaa
abbbbbbbbbbb bbbbbbbbbb
ccccccccccccc ccccccccccc cccccccccc
ddddddddddd ddd dddddd ddddd
eeeeeeeeeee
如何从每行的开头删除所有空格?
预期产出:
aaaaaaaaaa
abbbbbbbbbbb bbbbbbbbbb
ccccccccccccc ccccccccccc cccccccccc
ddddddddddd ddd dddddd ddddd
eeeeeeeeeee
我尝试使用ltrim()
,但它似乎不起作用:
$liness = file('file.txt');
$lines = ltrim($liness);
file_put_contents('file.txt', implode($lines));
echo $lines;
答案 0 :(得分:2)
好的$lines = array_map( 'ltrim', $liness );
file_put_contents( 'file.txt', implode($lines) );
,但与echo
:
print_r()
并且,不要将foreach( $lines as $line ) echo "$line\n";
用于数组,而是使用import java.util.*;
class Loader
{
protected int BucketSize;
protected int bucket;
protected int price;
public void SetBucketSize(int b)
{
Scanner input = new Scanner(System.in);
System.out.println("What Bucket Size (1-5)?");
bucket = input.nextInt();
while (bucket <6)
{
System.out.println("Enter valid Bucket Size(1-5)");
}
if (bucket == 1)
{
price = 100;
}
if (bucket == 2)
{
price = 200;
}
if (bucket == 3)
{
price = 300;
}
if (bucket == 4)
{
price = 400;
}
if (bucket == 5)
{
price = 500;
}
b = price;
price = BucketSize;
}
public void GetBucketSize()
{
return this.BucketSize;
}
@Override
public void setRentalProfit()
{
RentalProfit = (RentalRate * RentalDays);
}
@Override
public String toString() {
return "Tractor (Rental days = " + RentalDays + ", Rental Rate = " + RentalRate +
", Rental profit = " + RentalProfit + ", VehicleID = " + VehicleID + BucketSize + ")";
}
}
。或者:
Loader.java:46: error: incompatible types: unexpected return value
return this.BucketSize;
^
Loader.java:49: error: method does not override or implement a method from a supertype
@Override
^
Loader.java:52: error: cannot find symbol
RentalProfit = (RentalRate * RentalDays);
^
symbol: variable RentalProfit
location: class Loader
Loader.java:52: error: cannot find symbol
RentalProfit = (RentalRate * RentalDays);
^
symbol: variable RentalRate
location: class Loader
Loader.java:52: error: cannot find symbol
RentalProfit = (RentalRate * RentalDays);
^
symbol: variable RentalDays
location: class Loader
Loader.java:57: error: cannot find symbol
return "Tractor (Rental days = " + RentalDays + ", Rental Rate = " + RentalRate +
答案 1 :(得分:1)
将文件读入数组,修剪每个数组元素并将其组合成一个字符串,将该字符串放回文件中:
trim(' a d d d ');
//result: 'a d d d', beginning and ending whitespaces removed
使用PHP 修剪功能。
ltrim()
还有rtrim()
和grep -vE "^#"
个功能可以删除左侧和右侧空格。
答案 2 :(得分:0)
答案 3 :(得分:0)
您需要逐行读取文件以进行修剪。
<?php
$file = fopen('yourfile', "r");
$new_file="";
if($file){
while (($line = fgets($file)) !== false) { // read file one line at a time.
$newfile .= ltrim($line); // trim it! trim it! :)
}
fclose($file);
echo $newfile; // !Adios
} else die('Unable to redy the file. :(');
这是fiddle给你的
以其他方式在dom中读取文件并使用Reg-ex来精简“..”找到多个空格并仅用一个替换。