gpt4 book ai didi

php - preg_replace 引号内的所有换行符

转载 作者:行者123 更新时间:2023-12-02 22:01:46 24 4
gpt4 key购买 nike

<分区>

我正在尝试用带引号的字符串替换任何换行符,例如

$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"';

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com