Problems trying to explode a string - php

I am trying to split a text into an array using explode, but for some reason that does not work when the text is coming from a posted form.
If I run explode('|§|', 'qwe|§|asd|§|zxc'); I will get an array like:
Array
(
[0] => qwe
[1] => asd
[2] => zxc
)
BUT
If this input text comes from a form define like:
<form method="post">
Input: <input type="text" name="query" size="50" value="qwe|§|asd|§|zxc"><input type="submit" value="Parse">
</form>
I am getting the following array:
Array
(
[0] => qwe|§|asd|§|zxc
)
Im guessing this has to do with iso settings and that the text in the 'query' field has been altered in some way, but I can't understand how to fix. I have tried setting <meta http-equiv="Content-Type" content="text/html;charset=ISO-8859-1" /> and other charsets, but to no avail.
Any ideas? Thanks in advance.

Just an idea: The § sign is probably be converted to url format. Try urldecode() the string first.

I'm probably mistaken on this, but § may be a unicode character, which PHP does not yet support. Thus, there may be some issues when transferring from the form to the script.
Have you tried changing it to something more... normal? Like if you did qwe|~|asd|~|zxc instead, or maybe qwe|+~+|asd|+~+|zxc if you're concerned about what somebody would enter

Related

non-English language issue in creating php array from CSV file

In PHP, I am trying to create an array from a CSV file in this way.
// CSV file:
$csv = array_map('str_getcsv', file("{$uploadPath}/{$myFile}"));
Of course it is working for me, but on thing I have to fixed. If I have non-English language characters in my CSV file, then those characters not showing correctly in PHP. That mean its showing question marks instead of showing original characters.
This is the output of $csv array:
Array
(
[0] => Array
(
[0] => SISP-0002
[1] => Harpic Floral 500ml
[2] => ???????? ??????? 500ml
[3] => 4792037107765
)
)
UPDATE:
I have set charset inside page tag as shown here.
<meta charset="utf-8">
And also I have saved my CSV file with unicode(UTF-8)
Can anybody tell me what would be the possible workaround to fix this issue.
This appears to be the definitive question and answers for UTF-8 and php UTF-8 all the way through

PHP do not parse array input name

Let's say that I have the following simple input text box:
<input type="text" name="Details[0]->Name" value="" />
Now the problem is, php translating the input name as array, and then ignore the rest name after the closing square bracket. So in print_r, it become:
Details => Array{
[0] => "Input"
}
What can I do to workaround this? Is there any unparsed $_REQUESTS?
N.B: If you noticed it, yes I am trying to use automatic input to class mapper as it has been done in Asp.net mvc.
Edit:
The additional solution requirement is that I can read raw array input from either GET, POST or multipart form requests.
For POST:
echo urldecode ( file_get_contents('php://input'));
For GET:
echo urldecode ($_SERVER['QUERY_STRING']);
Both the above give the output Details[0]->Name=testval
As for enctype='multipart/form-data', the unparsed data is not available in php. However, there is a solution of sorts given by this SO question: Get raw post data
Just replace Input with some another name like Inputcust and make new array
Details => Array{
[0] => "Inputcust"
}
and where ever you want to use again replace from Inputcust => Input

display data from an array in json form

i got an array from echo $posts
Array
(
[0] => Array
(
[id] => 14
[name] => اسطنبوليه
)
)
i have this array (part of the main array)and now i wish to convert it to json form. however i am not able to do so, i tried to convert the data through
echo json_encode ($posts);
but instead of original data i am getting an output u0627u0633u0637u0646u0628u0648u0644u064au0647n
can anyone tell how i can get the correct form
Please try echo json_encode($posts,JSON_UNESCAPED_UNICODE) (php 5.4 and above)
2 things. Firstly view source of the output in the browser and you should see it as a JSON encoded string rather than the interpreted version.
Secondly it looks like there is some character encoding issues as the string you're getting back is unicode. Make sure you have the encoding set right on your server and browser.

php read list from website into array

I have a list of products codes that i wish to read into an array using php.
the list is to be fetched from a website and has over 700 items looks something like this:
4310ABC
4590DEF
8950GHK
What i want to do is put every code into a php array like so:
php_array ( [0] => 4310ABC
[1] => 4590DEF
[2] => 8950GHK)
This is what i have:
$php_array = file_get_contents('http://anysite.net/product_codes.php');
print_r (explode("\n",$php_array));
But my result is :
Array ( [0] => 4310ABC
4590DEF
8950GHK)
I have tried explode, preg_split('/[\n\r]+/', $php_array); but nothing seems to do the trick. Can anyone give me some pointers? thanks!
The lines are separated by a br, so use this instead:
$php_array = file_get_contents('http://anysite.net/product_codes.php');
print_r (explode("<br>",$php_array));
Don't forget to change the br to however it's spelled within the document you are fetching, for example it's often spelled like this:
<br />
Which is the most correct way to write it.
It would depend how your php file is echoing out the three values, so I am not sure how it is interpreting line breaks. Try echoing out the values with no line breaks, but separated by some other character like '*' or something, and then explode them along that and see if that works.

POST data with double brackets missing bracket

When this monster is sent via POST
<input name="arrayname[index][string[name]]" value="321" />
this is what i get:
print_r($POST['arrayname']);
>> Array ( [index] => Array ( [string[name] => 321) )
Where is the secons bracket?
I can access it via:
echo $val['string[name'];
but thats just horrible.
The structure of the name should be kept if possible.
Its part of a large generic method and only in some cases theese names get generated. If its not possible to work with names like theese properly (it is but ugly like i mentioned above), i will have to change the whole form generating.
Incorrect : arrayname[index][string[name]]
Correct : arrayname[index][string][name]
You have to choose:
Let PHP convert your POST data to arrays using the square bracket syntax
Parse POST data yourself from raw post data
You just cannot have both at the same time.
<input name="arrayname[index][string][name]" value="321" />

Categories