Decode URL parameters having special characters - php

I have a URL like this, i want to get value of variable "json" but i am not getting values after & parameters from "Hindi : L&W 3rd poem 3 times. Soc Science : L&W hard words 5 times.". How to get value of json correctly??
Note: I am getting value of json from app side so i cannot encode string before converting it into json string.
http://localhost/abc/sendMsg.php?json={"studentId":"15","msg":"Hindi : L&W 3rd poem 3 times. Soc Science : L&W hard words 5 times."}

From what I understand, the app in the client's browser generates the JSON array. In that case, you should try using JSON.stringify
<script>
url = "http://localhost/abc/sendMsg.php?json="+JSON.stringify(yourJSON);
</script>
EDIT
Alright, so from your comment you told be you're receiving the URL in PHP.
NOTE: The best practice would be to escape the URL from Android. If it is not escaped before sending, it will cause problems with some browsers/apps that might not support special characters. See this thread for the specific RFC: space in url; did browser got smarter or server?
If not possible as a fallback, I would advise splitting the URL to only retrieve the string, instead of using the $_GET parameter.
You could try something like:
<?php
$urlParts = explode('?json=', 'http://localhost/abc/sendMsg.php?json={"studentId":"15","msg":"Hindi : L&W 3rd poem 3 times. Soc Science : L&W hard words 5 times."}');
$jsonPart = end($urlParts);
$decodedJson = json_decode($jsonPart);
?>

please use below code
$str='[{"studentId":"15","msg":"Hindi : L&W 3rd poem 3 times. Soc Science : L&W hard words 5 times."}]';
print_r(json_decode($str));
output
Array ( [0] => stdClass Object ( [studentId] => 15 [msg] => Hindi : L&W 3rd poem 3 times. Soc Science : L&W hard words 5 times. ) )

convert it to an array after getting It by Using $_Request[]
then Use for each loop to get specific value
foreach ($json_array as $key => $value){
$hindi = $value->Hindi;
echo $hindi;
}

Related

TextArea joins as one string to PHP how can I set the other strings in new line with help of delimiter

I've tried searching all over google and tried a similar problem and answers yet I still didn't get my problem done.
So basically, I'm making an HTML to write user input in a textbox which is the user would input:
Dog,
cat,
coronavirus,
Fever,
Cough,
Then into my PHP code, I capture it with:
$input = $_GET['contents'];
So I tried this one which I saw from googling and it doesn't have an explanation but logically its about array:
$input= explode(array(",", ""), $input)[0];
It works but the problem is that it only shows the first output which is:
Dog
Dog
Dog
Dog
Dog
But when I tried to remove the delimiter code, it shows one string only:
Dog,cat,coronavirus,Fever,Cough,
I thought about it something related to array but I'm not sure, is there any way to display all of them per line and with the help with delimiter?
UPDATE:
So I tried doing print_r($secs); and it shows like this:
Array ( [0] => Dog [1] => cat )
Array ( [0] => )
My accomplishment is to read all of the arrays one by one in another line instead of joining it as one strings. Sorry for bad english and im trying my best to explain.
you do not need the array() line.
Instead
if(strpos($_GET['contents'],',') !== false) { //check for commas first
$input = explode(',' , $_GET['contents']);
echo '<pre>';
print_r($input); //gives you the array
echo '</pre>';
}
to access each string individually, call the offset. For example, print_r($input[0]);
Also a friendly piece of advice. You seem like a beginner. You do not have to execute multiple commands in one line. Write your code step by step.

How to extract the name from text in PHP

I want to extract the name from a paragraph or text content. I am using PHP. I tried to extract the name from below library.
https://packagist.org/packages/php-text-analysis/php-text-analysis
https://packagist.org/packages/php-text-analysis/php-text-analysis
$text = "my name is maneesh, and my friend name is Paritosh";
$freqDist = freq_dist(tokenize($text));
print_r($freqDist); die;
My expected output is : maneesh, Paritosh
Actual result is getting only frequency of word:
(
[my] => 2
[name] => 2
[is] => 2
[maneesh] => 1
[and] => 1
[friend] => 1
[Paritosh] => 1
)
If you are going to use the library you mentioned, you have to train your model. That means, fill them with many possible ways in which people can say their name. But even so, I wouldn't be perfect (depends on how well you trained your model).
Moreover, you are getting only frequency of words because that's the analysis you requested with the method freq_dist. I think you have to use corpus analysis for what you want.

php sscanf doesnt parse string properly

I develop ADIF parser and parsing process comes to the point where I use sscanf() php function The strind that I parse is as following: "QSO_DATE:8:D>20070909" and I need to draw info from here as following: "QSO_DATE", "8", "20070909" so I use code:
sscanf("QSO_DATE:8:D>20070909", "%s:%d:D>%d")
But returning array looks like this:
Array
(
[0] => QSO_DATE:8:D>20070909
[1] =>
[2] =>
)
What is wrong? maybe there is more efficient way to parse bunch of records like these:
<CALL:7>EM200FT<QSO_DATE:8:D>20140324<TIME_ON:4>1657<BAND:3>12M<MODE:5>PSK63<RST_SENT:3>599<RST_RCVD:0><QSL_SENT:1>Y<QSL_SENT_VIA:1>E<APP_EQSL_AG:1>Y<GRIDSQUARE:6>KN45kj<EOR>
<CALL:5>9V1SV<QSO_DATE:8:D>20140328<TIME_ON:4>1019<BAND:3>10M<MODE:4>JT65<RST_SENT:6>VK4CMV<RST_RCVD:0><QSL_SENT:1>Y<QSL_SENT_VIA:1>E<QSLMSG:54>Thank you and I confirm your SWL report, 73's de Siva.<APP_EQSL_AG:1>Y<GRIDSQUARE:6>OJ11ui<EOR>
<CALL:5>RA6DQ<QSO_DATE:8:D>20140328<TIME_ON:4>1019<BAND:3>10M<MODE:4>JT65<RST_SENT:3>599<RST_RCVD:0><QSL_SENT:1>Y<QSL_SENT_VIA:1>E<QSLMSG:3>73!<APP_EQSL_AG:1>Y<GRIDSQUARE:6>KN85nf<EOR>
%s means any characters, including colons, digits, chevrons, etc, except whitespace characters) and sscanf uses a greedy grab.... using more precise alternatives like %[A-Z_] or %[^:] might serve you better that %s
$result = sscanf("QSO_DATE:8:D>20070909", "%[^:]:%d:D>%d");
var_dump($result);
Which uses %[^:] to scan for any character other than a :

JSONify Python to feed into PHP

========== EDIT: ==========
Based on the below question, and below answer to use JSON. I'm rephrasing the question.
How can I take data from boto dynamo and jsonify it?
Right now I have something like this:
adv = #my advertiser
ads = self.swfTable.scan(advertiser__eq = adv)
arr=[]
for a in ads:
arr.append(a)
str = []
for i in arr:
str += [json.dumps(fields) for fields in i]
if str is not []:
json.dumps([ str.to_json() for ad in str ])
How do I turn this into a nice JSON dump or otherwise send it to my php?
========== Original Question: ==========
Forgive me I'm new to PHP.
So I have a stringified array of objects.
Ex:
Array [{cat,bat},{mat,hat}] -> ["cat","bat","mat","hat"] (let's call this aList below)
If I know each object pair will have a length of two. Is the only way to reform this Array by parsing the string? Is there any clever PHP way to do this?
I'm trying to move data from python to PHP in this case and sending a printed array seemed like the best / most universal way for me to write the api connection.
Here is my solution in pseudocode:
aList = file_get_contents(myUrl)
splitList = aList.split(",") # is there anyway to exclude "," from being also noticed? ex "app,le","blueberry" should only split 1x?
objects=[]
newObject{}
for int i =0;i<splitList.len; i++
if i%2
newObject.append(splitList[i])
objects.append(newObject)
newObject = {}
else:
newObject.append{list[i]}
Is there are way to do this in fewer lines / more efficiently? Also as mentioned above: is there anyway to exclude "," from being also noticed? ex "app,le","blueberry" should only split 1x?
You really should consider cross-language serialization, like JSON or MessagePack. As an example, see docs for PHP's json_decode and Python's json.

How to use a textarea to get the csv file /delimited data?

I would like to use a textarea in html form to get the delimited data
for example:
The simple data is like the following
testA#testa.com peter USA
testB#testB.com Tony USA
testC#testC.com tom USA
testA#testa.com peter USA
testA#.com peter USA
The problems are:
How to check where is each line ends? (\n)?
How to do duplication checking (only for email)? (if 3 data each row, get 1,4,7,11...data, and array_unique?)
Should i restrict the deliminator symbol or i do something to check automatically?
What If space is deliminator , but at same time my other data eg. is using space e.g. Tony Hanks ?
Thank you for any kind of help
First I would split string by line ends:
$r = explode(PHP_EOL, $data); //data is your raw data from textarea
To check the delimiter, explode first line by all delimiters that are possible and check array count.
foreach( array(' ', ';', '/') as $delimiter) {
$x = explode($delimiter, r[0]);
if(count($x) == 3) {
break;
}
}
After that use proper delimiter with str-getcsv on raw data: http://www.php.net/manual/en/function.str-getcsv.php
What If space is deliminator , but at same time my other data eg. is using space e.g. Tony Hanks ?
In that case you need to use quotes. Excel also could not handle this without quotes.
How to do duplication checking (only for email)?
Create array where keys are emails. Iterate through your parsed csv and check if key isset already or not.

Categories