I tried the sprintf function in php, but the results were not as expected, add an undesirable character:
JavaScript
x
https://localhost/item?item_id=abcd&redirect=https://www.google.com/×tamp=1616847526
unwanted is ×
the results I expected:
JavaScript
https://localhost/item?item_id=abcd&redirect=https://www.google.com/×tamp=1616847657
This is my code.
JavaScript
public function generateUrl()
{
$url = sprintf(
'%s%s?item_id=%s&redirect=%s×tamp=%s',
'https://localhost',
'/item',
'abcd',
'https://www.google.com/',
$this->timestamp,
);
return $url;
}
If I replace the timestamp
string with something else the result will be fine. What’s wrong with timestamp
? how to solve it?
thanks.
Advertisement
Answer
Nothing is wrong with sprintf
, ×
is the HTML entity for the multiplication sign x
.
If you really need to display the generated URL as you’re doing now you can either use return htmlentities($url)
instead of returning the $url
directly, or by changing the sprintf
call to :
JavaScript
$url = sprintf(
'%s%s?item_id=%s&redirect=%s&timestamp=%s',
'https://localhost',
'/item',
'abcd',
'https://www.google.com/',
$this->timestamp,
);