FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


Replacement of hexadecimal values in PHP string





rudi
I have just of question if it is possible to replace hexadecimal values in string: for example in long string with windows 1250 encoding (XX) to 2 bytes value in UTF-8 (XX XX)?

Thank you for answers!
hexkid
rudi wrote:
I have just of question if it is possible to replace hexadecimal values in string: for example in long string with windows 1250 encoding (XX) to 2 bytes value in UTF-8 (XX XX)?


I think you want the reconv or iconv functions.
rudi
Thank you but I am not shure that this is right or the best solution. I just found http://si.php.net/manual/en/function.preg-replace.php but I am not shure how to type pattern and replacement info in hexa values (if it is possible).

<?php
$string = 'The quick brown fox jumped over the lazy dog.';
$patterns[0] = '/quick/';
$patterns[1] = '/brown/';
$patterns[2] = '/fox/';
$replacements[2] = 'bear';
$replacements[1] = 'black';
$replacements[0] = 'slow';
echo preg_replace($patterns, $replacements, $string);
?>

As in this example I would like to replace just 5 (strange and non visible) characters with their XX XX value in UTF-8 format.

Any other suggestions also welcome Smile
hexkid
rudi wrote:
I would like to replace just 5 (strange and non visible) characters with their XX XX value in UTF-8 format.


Is it always the same 5 input characters?
Do you know what to transform them to?

Test and change this script (remember str_replace() can take arrays as parameters)
Code:
<?php
$cs = rand(0, 1);

if (!$cs) header('Content-Type: text/html; charset=windows-1250');
else header('Content-Type: text/html; charset=utf-8');

if (!$cs) echo 'windows-1250<br><br>';
else echo 'utf-8<br><br>';

$s = "x\xC5x z\xC4\xB9z";
echo $s, '<br>';

if ($cs) echo str_replace("\xC5", "\xC4\xB9", $s);
?>
rudi
Thank you again hexkid!

Yes I know exactly hexa value of all characters in both format.
If I want to replace this values with string_replace, should I give as third parameter string in hexa format or php will know that he has to search hexa values in normal string (so conversion of string string on the fly)?
rudi
Yes it works "on the fly", thank you again hexkid!
hexkid
rudi wrote:
Yes it works "on the fly", thank you again hexkid!


Glad you have it working Smile
Related topics
PHP/Mysql - beginner!
handling the $_POST array
Excel SpreadSheet to Website
PHP ristriction on Output
The blue screen of death
Sluchawki
Get specific parts of a string
Error messages and php editing in a photo archive (fixed?)
hashing your passwords
execute php string
is it possible to get a html text box to load a .txt file?
The information is not displayed in Textarea
How to use tags for search
PHP String formatting
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.