I have a text field in the database with datatype also as text. It holds comments and stuff. Now when I read this and export in a csv if it finds a new line in that comment such as
This is a comment
This is another line
The csv import show "This is another line" in next line and thus mess up my data.
So far I have tried str_replace(), trim(). Still don't seem to do anything. I have looked for similar answers in stackoverflow but couldn't find one that suits my problem
Thanks
What are you searching for in the str_replace function? You should be able to search for "\r" and or "\n" the the given string. Examples are documented in the PHP str_replace documentation. Example links below:
http://php.net/manual/en/function.str-replace.php#example-4450
http://www.php.net/manual/en/function.str-replace.php#97374
If you convert your newlines to printf-style strings, you can avoid this problem.
<?php
$t='This is a comment
This is another line
';
print str_replace("\n","\\n",$t);
Output:
This is a comment\n\nThis is another line\n
Also look in to htmlspecialchars(). You need to protect other characters that could break your CSV.
Lastly, have a close look at fputcsv() to see if it'll do what you need. Might be better to use builtins than to roll your own.
Related
I have a string:
$string = '😂🧜♂️';
And i want to split in:
$array = ['1F602', '1F9DCU-200D-2642-FE0F'];
How can i do it?
I have already try to use some functions but they doesn’t works because they doesn’t split properly emojis with more then one unicode.
Thank you in advance!
I was about to write the code for splitting emojis using an emoji-unicode dictionary but fortunately the code already exists.
This repo contains everything you need.
You can either use it directly or explore the code and take what you want.
I spent hours looking at dozens of different solutions but none of them worked. I am receiving the content of a textarea in a php string and I would like to remove anything which is more than one blank line.
Example ok
Hi how are you,
// one blank line here so ok to keep
Not too bad thanks
Example not ok
hi how are you
// two lines (or more) here so we remove one and keep the other
not too bad thanks
Does someone know the proper preg_replace to use? Note that I don't want to modify the data (no nl2br() please) as it is easier for me to keep it raw (ios support).
Maybe you should try this:
preg_replace('/\n\r(\n\r)+/', "\n\r", $str);
You could try
preg_replace("/[\r\n]+/", "\n", $text);
It replaces one (or more) newline or carriage return with a single newline.
I'm generating a CSV file using PHP and opening it in Excel. Adding "\n" at the end of each record creates a new line and works perfectly, however I need to figure out a way to create a newline within a cell itself. The same functionality that alt+enter achieves when entering data manually into Excel. Does anyone have any insight as to how this can be performed? I have tried "\n\r", "\n", chr(10), none of which seem to work, just keep getting a complete new line instead of newline within the same cell.
What I want to achieve is a header that looks like this...
This is all in one row in Excel..
CELL
Start Date
End Date
Thank you for any help provided!
How are you creating the csv file?
If you're doing it correctly and using fputcsv, having a line break within a cell wouldn't cause an issue.
Documentation to fputcsv
I have tried "\n\r", "\n",
Use "\r\n" :) Check the wiki article about 'Newline'
But note that you'll have to use double quotes. " otherwise PHP will not handle meta characters.
I've asked this question before but I didn't seem to get the right answer. I've got a problem with new lines in text. Javascript and jQuery don't like things like this:
alert('text
text);
When I pull information from a database table that has a break line in it, JS and jQuery can't parse it correctly. I've been told to use n2lbr(), but that doesn't work when someone uses 'shift+enter' or 'enter' when typing text into a message (which is where I get this problem). I still end up with separate lines when using it. It seems to correctly apply the BR tag after the line break, but it still leaves the break there.
Can anyone provide some help here? I get the message data with jQuery and send it off to PHP file to storage, so I'd like to fix the problem there.
This wouldn't be a problem normally, but I want to pull all of a users messages when they first load up their inbox and then display it to them via jQuery when they select a certain message.
You could use a regexp to replace newlines with spaces:
alert('<?php preg_replace("/[\n\r\f]+/m","<br />", $text); ?>');
The m modifier will match across newlines, which in this case I think is important.
edit: sorry, didn't realise you actually wanted <br /> elements, not spaces. updated answer accordingly.
edit2: like #LainIwakura, I made a mistake in my regexp, partly due to the previous edit. my new regexp only replaces CR/NL/LF characters, not any whitespace character (\s). note there are a bunch of unicode linebreak characters that i haven't acknowledged... if you need to deal with these, you might want to read up on the regexp syntax for unicode
Edit: Okay after much tripping over myself I believe you want this:
$str = preg_replace('/\n+/', '<br />', $str);
And with that I'm going to bed...too late to be answering questions.
I usually use json_encode() to format string for use in JavaScript, as it does everything that's necessary for making JS-valid value.
I have a CSV parser, that takes Outlook 2010 Contact Export .CSV file, and produces an array of values.
I break each row on the new line symbol, and each column on the comma. It works fine, until someone puts a new line inside a field (typically Address). This new line, which I assume is "\n" or "\r\n", explodes the row where it shouldn't, and the whole file becomes messed up from there on.
In my case, it happens when Business Street is written in two lines:
123 Apple Dr. Unit A
My code:
$file = file_get_contents("outlook.csv");
$rows = explode("\r\n",$file);
foreach($rows as $row)
{
$columns = explode(",",$row);
// Further manipulation here.
}
I have tried both "\n" and "\r\n", same result.
I figured I could calculate the number of columns in the first row (keys), and then find a way to not allow a new line until this many columns have been parsed, but it feels shady.
Is there another character for the new line that I can try, that would not be inside the data fields themselves?
The most common way of handling newlines in CSV files is to "quote" fields which contain significant characters such as newlines or commas. It may be worth looking into whether your CSV generator does this.
I recommend using PHP's fgetcsv() function, which is intended for this purpose. As you've discovered, splitting strings on commas works only in the most trivial cases.
In cases, where that doesn't work, a more sophisticated, reportedly RFC4180-compliant parser is available here.
I also recommend fgetcsv()
fgetcsv will also take care of commas inside strings ( between quotes ).
Interesting parsing tutorial
+1 to the previous answer ;)
PS: fgetcsv is a bit slower then opening the file and explode the contents etc. But imo it's worth it.