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;
}
Related
This question already has answers here:
what is the efficient way to parse a template like this in php?
(3 answers)
str_replace() with associative array
(5 answers)
PHP - Replacing multiple sets of placeholders while looping through arrays
(1 answer)
How do I use preg_replace in PHP with {{ mustache }}
(1 answer)
How can I replace a variable in a string with the value in PHP?
(13 answers)
Closed 19 days ago.
How to write custom php function for replacing variable with value by passing function parameters?
$template = "Hello, {{name}}!";
$data = [
'name'=> 'world'
];
echo replace($template, $data);
function replace($template, $data) {
$name = $data['name'];
return $template;
}
echo replace($template, $data); must return "Hello, world!"
Thank You!
One way would be to use the built-in str_replace function, like this:
foreach($data as $key => $value) {
$template = str_replace("{{$key}}", $value, $template);
}
return $template;
This loops your data-array and replaces the keys with your values. Another approach would be RegEx
You can do it using preg_replace_callback_array to Perform a regular expression search and replace using callbacks.
This solution is working for multi variables, its parse the entire text, and exchange every indicated variable.
function replacer($source, $arrayWords) {
return preg_replace_callback_array(
[
'/({{([^{}]+)}})/' => function($matches) use ($arrayWords) {
if (isset($arrayWords[$matches[2]])) $returnWord = $arrayWords[$matches[2]];
else $returnWord = $matches[2];
return $returnWord;
},
],
$source);
}
demo here
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:
How substr function works? [closed]
(3 answers)
Closed 4 years ago.
I am trying to get "pa" from "a(bcdefghijkl(mno)pa)q".
This is my code for exampe:
$s = "a(bcdefghijkl(mno)pa)q";
$mystring = substr($s,14,15);
echo $mystring;
outputs is:
mno)pa)q
You have use right code but the second parameter is wrong. use this one below
$s = "a(bcdefghijkl(mno)pa)q";
$mystring = substr($s,14,2);
echo $mystring;
In substr function:
first parameter means from which position of string starts.
and the second parameter means how many characters you have want.
$s = "a(bcdefghijkl(mno)pa)q";
$mystring = substr($s,-4,2);
echo $mystring;
Try this
This question already has answers here:
Detect HTML tags in a string
(8 answers)
Closed 4 years ago.
I want to do a simple kind of test to see if a string contains any HTML.
In this case if the $string variable is test or <test> it returns no for me:
$string = 'test';
if(strpos($string,'<') !== 'false'){
echo 'no';
}else{
echo 'yes';
}
Is there a better way to check if a string contains HTML? I don't want to do anything to the string just check if it has HTML tags?
if($string != strip_tags($string)) {
// contains HTML
}
Took the answer from here
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