hassank1 Posted January 30, 2009 Share Posted January 30, 2009 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 More sharing options...
gevans Posted January 30, 2009 Share Posted January 30, 2009 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 More sharing options...
hassank1 Posted January 30, 2009 Author Share Posted January 30, 2009 ok but how can I loop the whole string,is there a regular expression or something that can extract only the number(s) and convert them using the above function .. .. Link to comment https://forums.phpfreaks.com/topic/143104-html-entities-to-hex/#findComment-750548 Share on other sites More sharing options...
gevans Posted January 30, 2009 Share Posted January 30, 2009 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 More sharing options...
hassank1 Posted January 30, 2009 Author Share Posted January 30, 2009 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 More sharing options...
gevans Posted January 30, 2009 Share Posted January 30, 2009 Sorry, U've never dealt with different languages with php so that is not a question I can help you with, I'm sure someone else will take a look for you. I'd recommend starting a new thread as this want might be ignored because of the number of posts... Link to comment https://forums.phpfreaks.com/topic/143104-html-entities-to-hex/#findComment-750569 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.