Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Shiva's avatar
Level 5

Converting string to ascii hex

I'm trying to convert certain symbols in my string into ascii hex. I've been looking but I can't seem to find anything.

The string that I need converted is this

$success = url('payfast-success');
$cancel = url('payfast-cancel');

merchant_id=10000100&merchant_key=46f0cd694581a&return_url='.$success.'&cancel_url='.$cancel.'&m_payment_id=01AB&amount='.$total.'&item_name=Test Item&item_description=A test product&email_confirmation=1&[email protected]&payment_method=eft

and the output is suppose to look like this

merchant_id=10000100&merchant_key=46f0cd694581a&return_url=http%3A%2F%2Flaravel-shop.test%2Fpayfast-success&cancel_url=http%3A%2F%2Flaravel-shop.test%2Fpayfast-cancel&m_payment_id=01AB&amount=1.05&item_name=Test+Item&item_description=A+test+product&email_confirmation=1&confirmation_address=test%40test.com&payment_method=eft

This is what my output looks like at the moment

merchant_id=10000100&merchant_key=46f0cd694581a&return_url=http://laravel-shop.test/payfast-success&cancel_url=http://laravel-shop.test/payfast-cancel&m_payment_id=01AB&amount=1.05&item_name=Test Item&item_description=A test product&email_confirmation=1&[email protected]&payment_method=eft
0 likes
4 replies
tokoiwesley's avatar

Try using urlencode() instead of url().

$success = urlencode('payfast-success');
$cancel = urlencode('payfast-cancel');
tokoiwesley's avatar

If you haven't found a solution yet, then try out the modification below (to my solution above);

$success = urlencode(url('payfast-success'));
$cancel = urlencode(url('payfast-cancel'));

Please or to participate in this conversation.