-2

How can i convert string 'Hello world' to '\x48\x65\x6c\x6c\x6f\x20\x57\x6f\x72\x6c\x64\x21' using php?

Thanks!

0

1 Answer 1

3

Should work

function strhex($string) {
  $hexstr = unpack('H*', $string);
  return array_shift($hexstr);
}

Update

Code below what you need

function strtohex($string)
{
  $string = str_split($string);
  foreach($string as &$char)
    $char = "\x".dechex(ord($char));
  return implode('',$string);
}

print strtohex("[0-9A-Za-z\+/=]*");
Sign up to request clarification or add additional context in comments.

7 Comments

almost but not quite. It gives 48656c6c6f20776f726c64. 3v4l.org/68Cho
echo(strhex('Hello World!')); returns '48656c6c6f20576f726c6421'
bin2hex already does this natively
@Devon how can i get \x48\x65\x6c\x6c\x6f\x20\x57\x6f\x72\x6c\x64\x21?? tell me
@FunnyCheese i updated my answer
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.