This question already has answers here:
Any way to return PHP `json_encode` with encode UTF-8 and not Unicode? [duplicate]
(5 answers)
Closed 7 years ago.
$obj = [{"name":"Hình Ảnh","url":"images"}];
$str = json_encode($obj);
result:
[{"name":"H\u00ecnh \u1ea2nh","url":"hinh_anh"}]
Why?
Why $str not is:
[{"name":"Hình Ảnh","url":"images"}];
Add your file this code
header('Content-type: text/html; charset=UTF-8') ;
and change
$str = json_encode($obj, JSON_UNESCAPED_UNICODE);
Related
This question already has answers here:
JSON: why are forward slashes escaped?
(5 answers)
Closed 2 years ago.
I am trying to add 2 Strings like this:
$response = array();
$target_dir = "uploads/";
$public_key = "f1vlje6378uh6ucok8sda1exo5lmbu";
$target_dir .= $public_key."/";
$response["target_dir created"] = $target_dir;
echo json_encode($response);
"target_dir created": "uploads\/f1vlje6378uh6ucok8sda1exo5lmbu\/"
Any ideas why this "\/" appear and not just "/" ?
It's the default behavior where it escapes the slash.
If you do not want to get the slashes to be escaped, you can do it like this:
echo json_encode($response, JSON_UNESCAPED_SLASHES);
Here's the documentation of json_encode() and the various options you can use: https://www.php.net/manual/en/function.json-encode.php
This question already has answers here:
PHP convert string to hex and hex to string
(8 answers)
Encoding/decoding string in hexadecimal and back
(2 answers)
Closed 4 years ago.
How can i convert string 'Hello world' to '\x48\x65\x6c\x6c\x6f\x20\x57\x6f\x72\x6c\x64\x21' using php?
Thanks!
Should work
function strhex($string) {
$hexstr = unpack('H*', $string);
return array_shift($hexstr);
}
Update
Code below what you need
function strtohex($string)
{
$string = str_split($string);
foreach($string as &$char)
$char = "\x".dechex(ord($char));
return implode('',$string);
}
print strtohex("[0-9A-Za-z\+/=]*");
This question already has answers here:
PHP JSON encode. Echo value [duplicate]
(3 answers)
Closed 5 years ago.
I have this string
{"CALL CENTER":"CALL CENTER"}
I need to print CALL CENTER and I have tried using
substr($mystring, strpos($mystring, ":") + 1)
It gives me "CALL CENTER"}
How can I remove the special character from the result?
Use json_decode with the assoc array flag set to true and then you can just access the data in the array instead of parsing the string yourself.
$jsonArray = json_decode('{"CALL CENTER":"CALL CENTER"}', true);
echo $jsonArray['CALL CENTER']; // CALL CENTER
$jsonArray = json_decode('{"CALL CENTER":"CALL CENTER 2"}', true);
echo $jsonArray['CALL CENTER']; // CALL CENTER 2
http://sandbox.onlinephpfunctions.com/code/7650e1b9e705318e31c2b02f44ed05f9ee201d13
You can use the trim() function:
$mystring = trim($mystring, '"}');
Documentation: http://php.net/manual/en/function.trim.php
This question already has answers here:
PHP ltrim behavior with character list
(2 answers)
Closed 8 years ago.
I have this code..
$homepage1 = 'datastring=/mac_project/portfolio/kitchen/images/03.jpg';
$trimmed = ltrim($homepage1, 'datastring=/mac_project');
echo $trimmed;
I get the output as folio/kitchen/images/03.jpg. It's missing the /port from the /portfolio directory.
Full output should've been /portfolio/kitchen/images/03.jpg
Why not do the simple str_replace() ?
$homepage1 = 'datastring=/mac_project/portfolio/kitchen/images/03.jpg';
$trimmed = str_replace('datastring=/mac_project','',$homepage1);
echo $trimmed;// "prints" /portfolio/kitchen/images/03.jpg
The second parameter for ltrim is for character_mask, which means all the chars in the list will be trimmed.
You could use str_replace(), or if you want to replace only at the beginning of the string by preg_replace():
$trimmed = preg_replace('~^datastring=/mac_project~', '', $homepage1);
This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
How to decode Unicode escape sequences like “\u00ed” to proper UTF-8 encoded characters?
$str = '\u0627\u0644\u0631\u0626\u064a\u0633';
How to transfer it into utf-8? thanks.
$str = '\u0627\u0644\u0631\u0626\u064a\u0633';
$converted = preg_replace('/\\\u([0-9a-z]{4})/i', '&#x$1;', $str);
echo $converted; // displays الرئيس
$str = '"\u0627\u0644\u0631\u0626\u064a\u0633"';
echo json_decode($str);