I tried the sprintf function in php, but the results were not as expected, add an undesirable character:
https://localhost/item?item_id=abcd&redirect=https://www.google.com/×tamp=1616847526
unwanted is ×
the results I expected:
https://localhost/item?item_id=abcd&redirect=https://www.google.com/×tamp=1616847657
This is my code.
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 :
$url = sprintf( '%s%s?item_id=%s&redirect=%s&timestamp=%s', 'https://localhost', '/item', 'abcd', 'https://www.google.com/', $this->timestamp, );