English 中文(简体)
php转而向半殖民地分界线
原标题:php turn line break into semicolon
  • 时间:2011-03-08 12:18:55
  •  标签:
  • php
  • csv

我有一只手脚:

software
hardware
educational
games
languages
.
.
.

我需要一份新文件:

software;hardware;educational;games;languages;....

我如何能够这样做?

我这样做了:

<?php
$one = file_get_contents( one.csv );

$patterns =" /\n/";

$replacements = ";";

$newone = preg_replace($patterns, $replacements, $one);
echo $newone;

file_put_contents( newone.csv , $newone );
?>

这增加了线末的半殖民地,但线的中断仍然存在。

最佳回答

简言之,没有一个人提到<编码>(<>>>>>>,以回报他需要的东西:

$cont = file( somefile.txt , FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
file_put_contents( somefile.csv ,implode( ; ,$cont));

2 lines of code without using slow regex

OR

如果你们需要较少的法典,那么,这里有一条法线,那么就象!

file_put_contents(
   somefile.csv ,
   implode(
       ; ,
      file( somefile.txt , FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES)
   )
);
问题回答

Here is how you can do this. Edit : tested this, works correct.

 <?php
    $row = 1;
    $readHandle = fopen("in.csv", "r"); // open the csv file
    $writeHandle = fopen("out.csv","w");
    $subArr = array();
    while (($data = fgetcsv($readHandle, 1000, "
")) !== FALSE) {
             $myStr = $data[0]; // this stores the zeroth column of each CSV row
             $subArr[] = $myStr;   // subArr contains all your words      
    }

    fputcsv($writeHandle,$subArr,";"); // it creates a CSV with single line seperated by ;
    fclose($readHandle);
    fclose($writeHandle);

    ?>

我猜测你可以找到一个<代码>preg_match_all(),把每一字母的字母数字词用在一个阵列中。

然后,你就坐在这个阵列上,显示它们增加了一个半殖民地。

作为一员,我将跑回麦地。

perl -p -i -e  s|(.*)
|$1;|m  one.cvs

Your file may have carriage returns. Try this:

$newone = str_replace("
",  ; , $one);

涵盖所有可能性:

<?php

$file =  data.csv ;

file_put_contents($file,  "software"
"hardware"
"educational"
"games"
"languages"
 );

$input_lines = file($file);

$output_columns = array();
foreach($input_lines as $line){
    $line = trim($line); // Remove trailing new line
    $line = substr($line, 1); // Remove leading quote
    $line = substr($line, 0, -1); // Remove trailing quote
    $output_columns[] = $line;
}

echo implode( ; , $output_columns);

Beware:该法典在投入文件中没有错误。 途径增加了一些验证。

我建议这样做:

<?php
$one = file_get_contents( one.csv );

$patterns ="/\r?\n/";

$replacements = ";";

$newone = preg_replace($patterns, $replacements, $one);
echo $newone;

file_put_contents( newone.csv , $newone );
?




相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签