I receive data like this:
$string = "\x01\x03\r\x08\xc0";
From this variable how to output (with echo) the same value?
For example on python, when I type:
$ print [string]
I get this
["\x01\x03\r\x08\xc0"]
Note:
Without do $string = '\x01\x03\r\x08\xc0' because I uses a server who receives data and the string variable is the data.
Can someone can help me?
when php interpreter reach the line:
$string = "\x01\x03\r\x08\xc0";
it automatically parse string between double quotes. so you can not get real string. but if you can guarantee that response you get is a valid hex, use function bin2hex(). in that case you can convert result of that function to what you want. something like:
$str = bin2hex($hex);
$real_str = '';
for($i=0; $i<strlen($str); $i+=2){
$real_str .= '\\x'.$str[$i].$str[$i+1];
}
echo $real_str;
Related
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
I am trying to use a base64 encoding of a json array inside an api request.
This is the json array:
{"action":"START_STREAMING","metricsList": [{"category":"HARDWARE","subcategory":"MACHINE","component":"DASHBOARD","metric":"STATUS"},":[{"category":"HARDWARE","subcategory":"MACHINE","component":"DASHBOARD","metric":"TIME"},{"category":"HARDWARE","subcategory":"MACHINE","component":"DASHBOARD","metric":"INFO"}],"duration":60}
This is my current solution:
$command = base64_encode({"action":"START_STREAMING","metricsList": [{"category":"HARDWARE","subcategory":"MACHINE","component":"DASHBOARD","metric":"STATUS"},":[{"category":"HARDWARE","subcategory":"MACHINE","component":"DASHBOARD","metric":"TIME"},{"category":"HARDWARE","subcategory":"MACHINE","component":"DASHBOARD","metric":"INFO"}],"duration":60});
$request = [
'command' => $command
];
I am pretty sure this isnt quite right. Any help?
Even though Base64 encoding is not an encryption, sometimes it can be
useful to pass data or parameters in a single string.
Base64 Encode for URL:
function b64link_encode($string){
$string = base64_encode($string);
$string = urlencode($string);
return $string ;
}
encode that data and pass
Base64 Decode for URL:
function b64link_decode($string){
$string = urldecode($string);
$string = base64_decode($string);
return $string ;
}
or you can do
see this
Certain characters in the URL are special, like + which stands for a space. To send arbitrary data via the URL, you need to URL escape it to avoid characters contained in the data being recognized as "special characters". Since you're putting the data into HTML, you also need to HTML-escape it to avoid characters in the URL-encoded data being recognized as special HTML characters. Hence:
$data = /* some data */;
$base64Data = base64_encode($data);
$urlData = urlencode($base64Data);
$htmlData = htmlspecialchars($urlData);
printf('<input type="hidden" value="%s" name="pass-it-on">', $htmlData);
I working at PHP Project With PHP Version 7.0.13
I was dealing with JSON lately, I have a JSON file that needs to be decode to PHP but before I decode the JSON, I need to clean some abstract string inside the file that JSON obtained inside, to clean the string using substr() to get the JSON.
when i write the code, like this:
$jsonraw = "\"{ JSON should be here, later }\"";
$cutstart = strpos($jsonraw, "{");
$cutend = strrpos($jsonraw, "\"");
$jsonclean = substr($jsonraw, $cutstart, $cutend);
echo $jsonclean;
The output is like this
{ JSON should be here, later }
But when the string is like this
$jsonraw = "\"some abstract string to remove { JSON should be here, later }\"";
The output is became like this
{ JSON should be here, later }"
As we can see there was a quote symbol " at the last of the string, I was trying to decrement the $cutend, like this $jsonclean = substr($jsonraw, $cutstart, --$cutend); and this to $cutend-1
Any help, I appreciate.
Sorry for my bad English
You can use preg_match to get the json from that string:
$string = "some abstract string to remove { JSON should be here, later }";
preg_match('/\{.*\}/', $string, $match);
var_dump($match[0]);
the result would be:
string(30) "{ JSON should be here, later }"
As the third parameter is the length of the string, you need to say that the length is the end position minus the start position...
$jsonclean = substr($jsonraw, $cutstart, $cutend-$cutstart);
I'm sending comma separated values through a URL (key, value). I'm encoding them with Javascript's escape() and then replacing the commas within each value with %2c . The problem is at the PHP end the commas that are encoded are turned into "," BEFORE explode() takes place and then my string containing commas is broken up and it doesn't save right.
How can I stop PHP from converting my encoded bits back into unencoded bits?
My JS for each input is:
fieldData += $(this).attr("id")+","+escape($(this).html()).replace(/,/g,"%2c")+",";
My PHP is:
$fieldData = explode(",", $_POST['fieldData']);
Tried (along with other things):
$fieldData = explode(",", urlencode($_POST['fieldData']));
I would suggest using base64encode/decode for this.
The javascript would look something like this: http://jsfiddle.net/Y6yuN/
<script src='http://javascriptbase64.googlecode.com/svn/trunk/base64.js'></script>
fieldData += $(this).attr("id")+","+escape(Base64.encode($(this).html()))+",";
The escape is for the trailing =
So you would end up with comma delimited base64 encoded strings.
On the PHP side:
$fieldData = explode(",", $_POST['fieldData']);
foreach ($fieldData as $k => $v){
$fieldData[$k] = base64_decode(urldecode($v));
}
Your post is not really well explained, but I think you want to decode the data passed by JS. So, the code should be:
$fieldData = explode(",", urldecode($_POST['fieldData']));
Try to write it better if I am wrong!
I have a text string that is set in a variable to a value like these:
$str = 'type=showall'
or
$str = 'type=showall&skip=20'
$str = 'type=showall&skip=40'
$str = 'type=showall&skip=60'
and so on.
I need to check to see if there is a "skip" value present in the string, and if so replace it with a new number that is stored in a $newSkip variable and keep the string the same except for the change to the skip value.
For example if the string was:
$str = 'type=showall&skip=20'
and
$newSkip = 40
then I would like this to be returned:
$str = 'type=showall&skip=40'
If there was no skip value:
$str = 'type=showall'
and
$newSkip = 20
then I would like this to be returned:
$str = 'type=showall&skip=20'
I'm fairly new to PHP so still finding my way with the various functions and not sure which one/s are the best ones to use in this scenario when the text/value you're looking for may/may not be in the string.
PHP has a handy function called parse_str() which accepts a string similar to the one you have, and returns an array with key/value pairs. You'll then be able to inspect specific values and make the changes you need.
$str = 'type=showall&skip=20';
// this will parse the string and place the key/value pairs into $arr
parse_str($str,$arr);
// check if specific key exists
if (isset($arr['skip'])){
//if you need to know if it was there you can do stuff here
}
//set the newSkip value regardless
$arr['skip'] = $newSkip;
echo http_build_query($arr);
The http_build_query function will return the array into the same URI format that you started with. This function also encodes the final string so if you want to see the decoded version, you'll have to send it through urldecode().
References -
parse_str()
http_build_query()