Note that there are some explanatory texts on larger screens.

plurals
  1. POpreg_replace all newlines within quotes
    text
    copied!<p>sorry if this seems like a simple question but I am trying to replace any newline characters that are with a quoted string e.g.</p> <pre><code>$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"'; </code></pre> <p>I have tried all sorts, the problem is I can't get rid of the line endings themselves otherwise the fgetcsv function returns a single array it needs to be line endings / newlines within the quotes.</p> <pre><code>$str = str_replace(PHP_EOL, '', $str); </code></pre> <p>Okay here's my code, download the csv</p> <pre><code>&lt;?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.'&amp;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) </code></pre> <p>And then the file that grabs the csv and prints it out at the moment...</p> <pre><code>&lt;?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 '&lt;pre&gt;'; foreach($csv as $line){ if(count($line) != 16){ print_r($line); } } echo '&lt;/pre&gt;'; </code></pre> <p>So to reiterate I am trying to go from this:</p> <pre><code>$str = '"this string has no new lines" "but this one does have new lines to strip out"'; </code></pre> <p>to:</p> <pre><code>$str = '"this string has no new lines" "but this one does have new lines to strip out"'; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload