简体   繁体   中英

How can I replace all hex using RegEx in PHP?

So I've been toying around with Regular Expressions, and my friend challenged me to write a script that replaced all hex within a string. He gave me a large file mixed with different characters and, of course, some hex strings.

Each occurrence of hex is preceded with \\x , so for example: \\x55 .

I thought it'd be pretty easy, so I tried out this pattern on some online regex tester: /\\\\x([a-fA-F0-9]{2})/

It worked perfectly.

However, when I throw it into some PHP code, it fails to replace it at all.

Can anyone give me a nudge into the right direction of where I'm going wrong?

Here's my code:

$toDecode = file_get_contents('hex.txt');
$pattern = "/\\x(\w{2})/";
$replacement = 'OK!';

$decoded = preg_replace($pattern, $replacement, $toDecode);

$fh = fopen('haha.txt', 'w');
fwrite($fh, $decoded);
fclose($fh);
<?php
  // grab the encoded file
  $toDecode = file_get_contents('hex.txt');

  // create a method to convert \x?? to it's character facsimile
  function escapedHexToHex($escaped)
  {
    // return 'OK!'; // what you're doing now
    return chr(hexdec($escaped[1]));
  }

  // use preg_replace_callback and hand-off the hex code for re-translation
  $decoded = preg_replace_callback('/\\\\x([a-f0-9]{2})/i','escapedHexToHex', $toDecode);

  // save result(s) back to a file
  file_put_contents('haha.txt', $decoded);

For reference, preg_replace_callback . Also, don't use \\w as it's actually translated to [a-zA-Z0-9_] . Hex is base-16, so you want [a-fA-F0-9] (and the i flag makes it case-insensitive).

Working example , minus the file part.

Your problem is that you have not escaped your backslashes in the PHP string. It needs to be:

$pattern = "/\\\\x(\\w{2})/";

...or:

 
 
 
 
  
  
  $pattern = '/\\\\x(\\w{2})/';
 
 
  

...with single quotes. - This actually suffers the same problem and requires the full double-escaped sequence

But \\w will match any perl word character, which is not just hex characters. I would use the character class [a-fA-F0-9] instead.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM