This question already has an answer here:
Escape line breaks in MySQL output
(1 answer)
Closed 9 years ago.
The column data is fed by a textarea html element in the web page ; so the user can enter linebreaks within it. When I put the column data inside an excel file then excel does not recognize the linebreak ( there is a "?" at the end of the first line ). So how to make it recognizable by excel ?
I ran into this problem a little earlier, The easiest solution (Not the best, but couldn't think of anything else) is to use str_replace().
The only way i found to get this to work correctly, was to replace \n with \n\r. An example of this would be
<?php
str_replace("\n", "\n\r", $input_text);
?>
It is possible to do this while inserting into the database, or while reading from the database, However it does present a problem that if it already has \n\r, it'll then appear like \n\r\r
Related
This question already has answers here:
How to loop through PHP object with dynamic keys [duplicate]
(16 answers)
Closed 5 years ago.
I am trying to make a card reader for cardcast. I get these cards in a string using file_get_contents and then need to extract the text for each card.
Here is an example piece of the string for one card, each one of these cards is separated by a comma and has the parentheses on either end.
{"id":"030b3406-55ae-4159-9129-04463c61973c","text":["Why does this not work? ",""],"created_at":"2017-06-04T15:13:42+00:00","nsfw":true}
I am trying to extract just the information inside the text tag, in this case ' Why does this not work? ","" ' (minus the single quotes and white space on both ends).
Could someone please help to extract this information using split or regex? I could do this slowly with split but I assume it is more efficient to use regex as there could be multi-hundred cards and this needs to be time efficient.
I looked into preg_match() but did not understand it well enough to get a functioning version.
Thanks
Looks like a JSON encoded text.
Simply decode the text using 'json_decode()'
JSON: JavaScript Object Notation.
JSON is a syntax for storing and exchanging data.
JSON is text, written with JavaScript object notation.
You can learn more about json here :
http://www.json.org/
https://www.w3schools.com/js/js_json_intro.asp
change json to php array with -> $array=json_decode($json,true)
get ur text attribute:
$array['text']
This question already has answers here:
UTF-8 all the way through
(13 answers)
Closed 7 years ago.
I have a database which contains some blocks of text. These text blocks contain extended characters such as: ’ ‘ … “ and ”. When displayed directly to a web page they all show like this: �.
I've tried doing as str_replace to show normal characters, with no luck.
I've tried iconv, which will only work when set to ignore, which makes the punctuation look wrong.
I've tried html_encode, which also doesn't work. (I'm also using the parsedown script to format the text.)
The funny thing is, the website I'm replacing supports these characters fine, so I don't know what I'm doing wrong! (I don't have access to this website, or source code, or database, which is why I'm replacing it!)
Can anyone provide any help??
I just want to stop showing � and start showing proper characters!
Thanks to the above linked article, this issue is now resolved.
I firstly changed the collation of all of my tables as follows:
Specify the utf8mb4 character set on all tables and text columns in
your database.
Then in my php code where it connects to the database, I added this line:
$CONNECTION -> set_charset('utf8mb4');
All issues resolved! Thanks to all who contributed to my fix!
This question already has answers here:
How can I prevent SQL injection in PHP?
(27 answers)
Closed 7 years ago.
I have never come across this before and it's completely stumped me!
I am using some custom front end posting scripts in Wordpress, these work absolutely fine, everything is escaped correctly before the data is saved to the db etc, but I seem to be having an issue with the apostrophe (single quote) when entered on a Mac keyboard.
An example post using various chars is entered in the text area :
a post with other chars `~":;?/[]{}-_=+!£$%^&*()'
And this is what is saved in the db (and then displayed on the page when called) :
a post with other chars `~":;?/[]{}-_=+!£$%^&*()'
This is how the data is being saved to the db for clarity :
esc_attr(strip_tags($_POST['postContent'])),
If I copy and paste an apostrophe from a webpage like this for example it saves just fine :
Ain’t that strange!
But that slanted apostrophe isn't available (as standard) on the Mac keyboard, the default is the single quote '
I have never come across this issue before.
Anybody got any suggestions or workarounds as I'm at my wits end!
Ok found it..
Instead of :
esc_attr(strip_tags($_POST['postContent'])),
Use the built in wordpress function sanitize_text_field :
'post_content' => sanitize_text_field($_POST['postContent']),
This question already has answers here:
when to use htmlspecialchars() function?
(4 answers)
Closed 9 years ago.
I need to convert my strings to special characters using:
htmlspecialchars
My question is, should I convert my data before submitting it to a database or should I convert it before I display it?
You should sanitize data before inserting it into a database, and escape it on retrieval.
htmlspecialchars is used for escaping, so it should be after you’ve fetched it from the database.
It makes the data safe to insert into an HTML document. Use it before you insert it into an HTML document, not a database.
It's generally the better idea to not modify source data before storing it. It will tie your data to the specific context you're using it in. What if you ever need a different way of displaying it, e.g. in a PDF, or text format? Then you will have the html entities in your text and would need to convert them back.
IMHO Performance considerations are secondary in this regards, one can still make use of caching technologies for views for this.
So, on the bottom line I suggest you always prepare your strings before display.
I'm assuming the data is already escaped sanitised before you put it into the database so it is safe. From there, I try to change the data as little as possible on the way to the database.
The thing to remember is that maybe you're using the copy now on your website, but later down the line you may like to use it on a different device or on print. If you use htmlspecialchars before it goes to the database, you'll have to clean it up if you want to use it for something other than HTML. Formatting dates as strings before putting them into a database is a common one, but when you want to change the format...
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Newline Conversion in Submitted Text in PHP
I'm using PHP to store text submitted from a textarea into a database to later be echoed out. If a user presses enter in the textbox to start a new line, I want it to start a line break.
How can I go about doing that?
Let's say this is the textbox:
This is line one.
This is line two.
When a user submits this to the database it is just stored as one line and echoed out is the same result.
"This is line one. This is line two."
So how do I make the stored text reflect the format of the original input?
You would run something like nl2br() on your output when reading FROM the database. The new lines will be stored as \n.
http://php.net/manual/en/function.nl2br.php
It should also be noted that you should not run nl2br() before inserting into the database. Just not a good practice.