我正在尝试用带引号的字符串替换任何换行符,例如
$help = '"Hi this is a string and I really want to replace
any newlines that are within that string" "There are multiple strings all within one string that all need
to have their newlines replaces"';
我已经尝试了各种。问题是我自己无法摆脱行尾。否则 fgetcsv 函数返回一个数组。它需要是引号内的行尾/换行符。
$str = str_replace(PHP_EOL, '', $str);
好的,这是我的代码。下载 csv 文件。
<?php
$username = 'username';
$password = 'password';
$loginURL = 'http://www.example.com/login';
$contentURL = 'http://www.example.com/feedback.csv';
// Initialize the curl
$ch = curl_init();
// Pass the curl some options
curl_setopt($ch, CURLOPT_URL, $loginURL);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, 'inp-email=' . $username . '&inp-pass=' . $password);
curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// Execute the curl to login
$store = curl_exec($ch);
// Change the URL to the CSV and execute
curl_setopt($ch, CURLOPT_URL, $contentURL);
$content = curl_exec($ch);
// Time to sanitise, first I want to remove any newlines from customers comments
$content = '\"' .implode('"', explode(PHP_EOL, $content)) . '\"';
// Return the file contents
file_put_contents('feedback.csv', $content)
然后是抓取 CSV 文件并将其打印出来的文件......
<?php
// Function to loop through CSV and build up array
function readCSV($csvFile){
$file_handle = fopen($csvFile, 'r');
while (!feof($file_handle) ) {
$csvlines[] = fgetcsv($file_handle, 0, "\t");
}
fclose($file_handle);
return $csvlines;
}
// Set path to CSV file
$csvFile = 'feedback.csv';
// Read the CSV file and build array using readCSV function
$csv = readCSV($csvFile);
echo '<pre>';
foreach($csv as $line){
if(count($line) != 16){
print_r($line);
}
}
echo '</pre>';
因此,重申一下,我正试图从这一点出发:
$str = '"this string has no new lines" "but this one does have new
lines to strip out"';
到:
$str = '"this string has no new lines" "but this one does have new lines to strip out"';