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
Related
This question already has answers here:
php prevent & creating codes in a string [duplicate]
(2 answers)
Closed 2 years ago.
Code:
function getShopInfo()
{
$url = $this->env['url'] . "shop/get?partner_id=" . $this->partner_id . "&shopid=" . $this->shop_id . "×tamp=" . $this->getTimestamp();
echo $url;
}
Result:
https://url.com/api/v2/shop/get?partner_id=99999&shopid=123456×tamp=1613629442
Why the output for ×tamp is printed as ×tamp?
& has special meaning in HTML, it's used to start an entity specification.
Use the htmlspecialchars() function to encode all the special characters so you can see them literally.
echo htmlspecialchars($url);
Better yet, you should be using http_build_query() to build URL-encoded query strings. i.e:
function getShopInfo()
{
$data = [
"partner_id" => $this->partner_id,
"shopid" => $this->shop_id,
"timestamp" => $this->getTimestamp()
];
$url = $this->env['url'] . "shop/get?". http_build_query($data);
echo $url;
}
This question already has answers here:
How do I replace certain parts of my string?
(5 answers)
Closed 2 years ago.
i have file names strings like this :
Admin_studies.php
Studies_lang.php
and i want to replace it like this :
Admin_classes.php
Classes_lang.php
which keep the case as it's and just replace the sting word
i tried something like this , but it will not keep the original chars cases
$newname = "classes";
$oldname = "studies";
$file_name = "Admin_studies.php"; //or may be $file_name = "Studies_lang.php";
echo preg_replace("/($oldname)/i","$1",$file_name );
You can set arrays for replacement templates
$newnames = ['classes', 'Classes'];
$oldnames = ['studies', 'Studies'];
$file_name = "Admin_Studies.php";
echo str_replace( $oldnames, $newnames, $file_name );
This question already has answers here:
PHP parse/syntax errors; and how to solve them
(20 answers)
Closed 4 years ago.
I have string in PHP like below
C:\xampp\htdocs
I want output of it using str_replace like
/xampp/htdocs
I am trying it like below
$path = getcwd();
$new = str_replace(array("C:", "\"), ("","/") $path);
echo $new;
but its giving me error like below
Parse error: syntax error, unexpected '","' (T_CONSTANT_ENCAPSED_STRING), expect
ing ')' in C:\xampp\htdocs\install-new.php on line 16
Let me know what is wrong with it.
It's because you escaped a double quote.
You need to add an backslash to your second expression like that:
$path = getcwd();
$new = str_replace(array("C:", "\\"), ("","/") $path);
echo $new;
You are missing array declaration on the 2nd argument, as well as a comma before the 3rd argument $path. Lastly as noted in the comments, the \ needs to be escaped otherwise it escapes the closing quote:
This:
$new = str_replace(array("C:", "\"), ("","/") $path);
Should be:
$new = str_replace(array('C:', '\\'), array('','/'), $path);
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:
What is the difference between single-quoted and double-quoted strings in PHP?
(7 answers)
Closed 7 years ago.
What is the correct form to make $id behave as a PHP variable inside the str_replace command? I've tried wrapping the $id inside with {, or ., but nothing helped. I'm not even sure how to define the problem I'm having here so I didn't really know how to Google this:
$id="Something";
$new = str_replace('?abc', '?id=$id&abc', $original);
There are two ways to concatenate strings in PHP. In your example, it would be either:
$new = str_replace('?abc', '?id=' . $id . '&abc', $original);
or
$new = str_replace('?abc', "?id=$id&abc", $original);
Note that the first option is slightly more efficient, and the spaces are optional.
i think it needs to be work like this.
$search = 's';
$replace = 'r';
$subject = 'subject';
$result = str_replace($search,$replace,$subject);
var_dump($result);
and the result will be 'rubject'.