After Explode() geting result: %C4%84 - php

My problem is that after exploding a string I get this: %C4%84 value
but I should get Ą. I can convert it using preg_replace, but is there simple way to convert ?
$band_song = explode('/',$url,6);
echo $band5=$band_song[3].' '.$band_song[4]);

You can simply urlencode your variables.
$var = urlencode($original_var);
EDIT
You'd want decode, not encode sorry
$var = urldecode($original_var);

Related

Decode string with \x in PHP that has char encoded over 255

My problem is about string decoding.
Let's assume a string like:
$str = "\xce\xbb\xc6\x9b\xc2\xac\xe2\x88\xa7\xe2\x9f\x91\xe2\x88\xa8\xe2\x9f\x87\xc3\xb7 \xe2\x82\xac\xc2\xbd\xe2\x88\x86\xc3\xb8\xe2\x86\x94\xc2\xa2\xe2\x8c\x90\xc3\xa6";
I want to decode it and to look like that:
λƛ¬∧⟑∨⟇÷ €½∆ø↔¢⌐æ
I tried to use
utf8_encode(utf8_encode($str));
But it's not what was expected.
In python something like that works:
_str = b"\xce\xbb\xc6\x9b\xc2\xac\xe2\x88\xa7\xe2\x9f\x91\xe2\x88\xa8\xe2\x9f\x87\xc3\xb7 \xe2\x82\xac\xc2\xbd\xe2\x88\x86\xc3\xb8\xe2\x86\x94\xc2\xa2\xe2\x8c\x90\xc3\xa6"
_str = _str.decode()
print(_str)
You don't need to decode that. This is legal notation for strings in PHP.
$str = "\xce\xbb\xc6\x9b\xc2\xac\xe2\x88\xa7\xe2\x9f\x91\xe2\x88\xa8\xe2\x9f\x87\xc3\xb7 \xe2\x82\xac\xc2\xbd\xe2\x88\x86\xc3\xb8\xe2\x86\x94\xc2\xa2\xe2\x8c\x90\xc3\xa6";
echo $str; //λƛ¬∧⟑∨⟇÷ €½∆ø↔¢⌐æ
https://3v4l.org/0e0Po

Convert string like this to php array?

I want to convert a string like this code below to array, How can I do this plz?
$icon = '{``font``:``feather``,``icon``:``feather-facebook``}';
Is there any way by using json_decode or something like this?
Thanks.
The input string is not proper json format
When you correct json syntax then you can just json_decode it
$icon = '{"font":"feather","icon":"feather-facebook"}';
$array = json_decode($icon);
var_dump($array);

How to replace string from getting postman as parameter

Currently i am facing issue of replace string , I used postman & passing string in postman like [{"id":"115","flag":"1","qty":"3","size":"10"}] as a parameters but when i print string i am getting output like [{\"id\":\"115\",\"flag\":\"1\",\"qty\":\"3\",\"size\":\"10\"}] , So i want to only remove '\' from string i have tried following code but not work.
$fliesid_in_store = $_REQUEST['fliesid_in_store'];
echo $res = preg_replace("/[^a-zA-Z]/", "", $fliesid_in_store);
Have you tried stripslashes.
$fliesid_in_store = $_REQUEST['fliesid_in_store'];
echo stripslashes($fliesid_in_store);
The string you mentioned is in json format
$json = [{"id":"115","flag":"1","qty":"3","size":"10"}] //this is json
Assign that to variable and decode it.
$string = json_decode($json,TRUE) this give result in array format
In your case
$string = json_decode($_REQUEST['fliesid_in_store'],TRUE);

How can I convert a string to array?

I have a string like this:
$str = '[{"file_id":"AgADBAADX6oxGyqs0FJLW3rZ3g6_fDnO-RkABB0pg6HTwdv7EqUBAAEC","file_size":1347,"file_path":"photos\/file_2.jpg","width":90,"height":75},{"file_id":"AgADBAADX6oxGyqs0FJLW3rZ3g6_fDnO-RkABIMbRhad2WVdE6UBAAEC","file_size":17588,"width":320,"height":265},{"file_id":"AgADBAADX6oxGyqs0FJLW3rZ3g6_fDnO-RkABHSo-WKlRRfBEaUBAAEC","file_size":18480,"width":330,"height":273}]';
How can I access items in it?
I can use regex to select them, something like /"file_id":"(.*?)"/. But that's not clean at all. Is there any approach to make a array (or an object) of string above?
It's a json string.
You need to decode it with json_decode.
The second argument (true) is to make it an array.
$str = '[{"file_id":"AgADBAADX6oxGyqs0FJLW3rZ3g6_fDnO-RkABB0pg6HTwdv7EqUBAAEC","file_size":1347,"file_path":"photos\/file_2.jpg","width":90,"height":75},{"file_id":"AgADBAADX6oxGyqs0FJLW3rZ3g6_fDnO-RkABIMbRhad2WVdE6UBAAEC","file_size":17588,"width":320,"height":265},{"file_id":"AgADBAADX6oxGyqs0FJLW3rZ3g6_fDnO-RkABHSo-WKlRRfBEaUBAAEC","file_size":18480,"width":330,"height":273}]';
$arr = json_decode($str, true);
Var_dump($arr);
https://3v4l.org/9BFIC
Explode(“,{”, $str); will work for the above.
You will get array value for each file.

Convert a GET variable containing numbers range to string in PHP

I have a get variable in this format : 0-1499. Now I need to convert it to a string so that I can explode the variable. For this I tried to convert it to string , but I am not getting any output. Here is the sample code :
$mystring = $_GET['myvars']; //equals to 0-1499;
//$mystring = (string)$mystring;
$mystring = strval($mystring);
$mystring = explode("-",$mystring);
print_r($mystring);
The above print_r() shows an array Array ( [0] => [1] => 1499 ). That means it calculates the $mystring before converted into string. How can I send 0-1499 as whole string to explode ?
I have a get variable in this format : 0-1499
When you grab this variable from the URL say.. http://someurl.com/id=0-1499
$var = $_GET['id'];
This will be eventually converted to a string and you don't need to worry about it.
Illustration
FYI : The above illustration used the code which you provided in the question. I didn't code anything extra.
You need quotes, sir.
Should work fine like this.
$mystring = "0-1499";
$mystring = explode("-",$mystring);
print_r($mystring);
Without the quotes it was numbers / math.
0 minus 1499 = negative 1499
As you correctly note it treats the value as arithmetic and ignores the 0- part. If you know that the value you'll get is 0-n for some n, all you need to do is this:
$mystring="0-".$n;
$mystring=explode("0-", $mystring);
but explode here is a bit redundant. So,
$myarr=array();
$myarr[1]=strval($mystring);
$myarr[0]="0";
There you go.
Explode is used for strings.http://php.net/explode
<?php
$mystring = "0-1499";
$a=explode("-",$mystring);
echo $a[0];
echo "<br>";
echo $a[1];
?>
see it working here http://3v4l.org/DEstD

Categories