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']
Related
This question already has answers here:
What kind of string is this? How do I unserialize this string? [duplicate]
(2 answers)
Closed 7 years ago.
So I've got a series of what seem to be arrays coming out of CSV document. The farthest I can break it down is to this:
a:11:{i:0;a:6:{s:4:"name";s:10:"First Name";s:5:"value";s:4:"Mark"}i:10;b:0;}
I'm not sure how to get these strings/arrays to be at associative arrays.
This is a serialized PHP array. You can use unserialize to restore the array. After that $array[0]["name"] will equal to First Name for example.
That's PHP's serialize format. Step 1, complain to whoever sent you the file for using a format not intended for interchange; step 2, use unserialize to decode it.
I have a string "test#html.com+test+7".I want to extract test#html.com, test and 7 into an array. How to achieve this is PHP? I have tried using preg_match but couldn't crack it.
print_r(explode("+","test#html.com+test+7"));
working code :)
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
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:
Sitemap urls with special characters [closed]
(6 answers)
Closed 9 years ago.
I want to submit my sitemap to Google, but I don't want to mess anything up. I am also having trouble with the URLs to submit; some of them have special characters in them such as the ampersand (&) symbol and parenthesis (). I just want to know what is the correct way to handle them?
I am currently using PHP's urlencode(), which turns them in to %28, %29 and so on which doesn't really look too good and I am scared if I give Google those links and they go on to index them they will index them as
domain.com/blabla%28blabla.html
Rather than
domain.com/blabla&blabla.html
Are you generating the XML by hand? Please consider using something like the PHP DOM classes instead.
You'll actually want to encode ampersands as &, etc., but it's really best to let a library emit well-formed XML for you.
See Generating XML document in PHP (escape characters) for more discussion of this.
urlencode() is the right function to use. You definately don't want ampersands in your URL because they are a special character used to form a URL (for passing GET variables).