Jump to content

html entities to hex.


hassank1

Recommended Posts

I want to enter an arabic string in a text box. so it will be converted to something like that :

 

ex: بس

 

how can I convert the numbers above to hexadecimal and the &# to '0' ..

 

I want the result to be something like that

 

$input = "بس"
$output = "06280633"

Link to comment
https://forums.phpfreaks.com/topic/143104-html-entities-to-hex/
Share on other sites

Will the format ever change, this are a fw ways to go about this, but if "بس" might change to "76;س" or "بسبسبس" one way will be better than the other....

 

To get you started dechex() returns a hexadecimal value from a decimal so;

 

<?php
$your_number = 1576;
$hex_number = dechex($your_number);
echo $hex_number;//echos 628

Link to comment
https://forums.phpfreaks.com/topic/143104-html-entities-to-hex/#findComment-750539
Share on other sites

My regex isn't amazing, but this should work;

 

$subject = "بس";

$pattern = "/()([0-9]+);/";

preg_match_all($pattern, $subject, $matches);

echo $matches[1][0].'<br />';
echo $matches[1][1].'<br />';
echo $matches[2][0].'<br />';
echo $matches[2][1].'<br />';

 

I've got all the array values (that you require) printed out afterwards so that you can see what's happened. After that you can just loop through them;

 

$finalPart = NULL;
for($i=0;$i<count($matches[0]);$i++) {
$firstPart = ($matches[1][$i] == '')? 0 : '';
$secondPart = dechex($matches[2][$i]);
$finalPart .= $firstPart . $secondPart;
}
echo $finalPart;

 

So when you put it altogether;

 

<?php
$subject = "بس";
$pattern = "/()([0-9]+);/";
preg_match_all($pattern, $subject, $matches);

$finalPart = NULL;
for($i=0;$i<count($matches[0]);$i++) {
$firstPart = ($matches[1][$i] == '')? 0 : '';
$secondPart = dechex($matches[2][$i]);
$finalPart .= $firstPart . $secondPart;
}
echo $finalPart;
?>

Link to comment
https://forums.phpfreaks.com/topic/143104-html-entities-to-hex/#findComment-750557
Share on other sites

thx I will check that

however there's still something

I've a textbox (arabictxt) where user can enter an arabic txt..  when I submit .. I echo $_POST[arabictxt] .. the variable

shows me the arabic text.. however I want the variable to be in this format : ჶჷ.... etc.. so I can

do the above operations on it..

 

how can I do that ..thx

Link to comment
https://forums.phpfreaks.com/topic/143104-html-entities-to-hex/#findComment-750560
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.