Jump to content

Convert PHP function to Autoit ?


Recommended Posts

Hello sirs,

recently i have problem converting some facebook API result to UTF 8, i found this PHP function which works great, but it would be so great if autoit can do same kind of job, i would be grateful if some one can help. thank in advance

<?php
 $a='Bu\u00f4\u0300n c\u01b0\u01a1\u0300i ch\u00ea\u0301t \u0111\u01b0\u01a1\u0323c :v';
 
 
 echo $str = preg_replace_callback('/\\\\u([0-9a-fA-F]{4})/', function ($match) {
    return mb_convert_encoding(pack('H*', $match[1]), 'UTF-8', 'UCS-2BE');
}, $a);
 
 
?>

 

Link to comment
Share on other sites

Looks like preg_replace_callback is essential two calls in autoit,

one is a StringReplace() (using StringRegEx) and then this calls a function that has the replaced string as an argument

The Second mb_convert_encoding is converting that replaced string to a different character encoding. I dont know much about this but I found a topic with:

 

Func Asc2Unicode($input)
    If StringLen($input) <> 1 Then Return SetError(-1, -1, -1)
    Local $FullUniStr = DllStructCreate('byte[3]')
    Local $Buffer = DllStructCreate('byte[2]', DllStructGetPtr($FullUniStr) + 2)
    Local $Return = DllCall('Kernel32.dll', 'int', 'MultiByteToWideChar', _
            'int', 0, _
            'int', 0, _
            'str', $input, _
            'int', StringLen($input), _
            'ptr', DllStructGetPtr($Buffer, 1), _
            'int', 2)
    DllStructSetData($FullUniStr, 1, 0xFF, 1)
    DllStructSetData($FullUniStr, 1, 0xFE, 2)
    Local $temp = DllStructGetData($Buffer, 1)
    Return '\u' & StringMid($temp, 5, 2) & StringMid($temp, 3, 2)
EndFunc   ;==>Asc2Unicode

The encoding you want:

FE FF = UCS-2 (16bit) big endian

so I THINK! that the above should be converted to 

Func Asc2Unicode($input)
    If StringLen($input) <> 1 Then Return SetError(-1, -1, -1)
    Local $FullUniStr = DllStructCreate('byte[3]')
    Local $Buffer = DllStructCreate('byte[2]', DllStructGetPtr($FullUniStr) + 2)
    Local $Return = DllCall('Kernel32.dll', 'int', 'MultiByteToWideChar', _
            'int', 0, _
            'int', 0, _
            'str', $input, _
            'int', StringLen($input), _
            'ptr', DllStructGetPtr($Buffer, 1), _
            'int', 2)
    DllStructSetData($FullUniStr, 1, 0xFE, 1)
    DllStructSetData($FullUniStr, 1, 0xFF, 2)
    Local $temp = DllStructGetData($Buffer, 1)
    Return '\u' & StringMid($temp, 5, 2) & StringMid($temp, 3, 2)
EndFunc   ;==>Asc2Unicode

 

Link to comment
Share on other sites

Converting \u**** sequences into AutoIt (Unicode) is done like that:

Local $string = 'Bu\u00f4\u0300n c\u01b0\u01a1\u0300i ch\u00ea\u0301t \u0111\u01b0\u01a1\u0323c :v'
$string = Execute('"' & StringRegExpReplace($string, '\\u([[:xdigit:]]{4})', '" & ChrW(0x$1) & "') & '"')
MsgBox(0, "", $string)

Are you sure you then need to convert the string to UTF8? AutoIt string are Unicode UTF16-LE, not UTF8. But if you actually need to do so, you can use this:

$UTF8string = BinaryToString(StringToBinary($string, 4), 1)

Note: the functions posted as examples a few posts above are incorrect.

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

 

Out of curiosity, What was wrong with the code above? I was able to write an example that converted a string to \u codes. The function posted above converts 1 character at a time.

 

was the encoding backwards? \u codes -> ascii instead of ascii -> \ucodes

 

Edited by Shane0000
Link to comment
Share on other sites

I was rather refering to the functions in the post linked to.

Then the OP wanted \uXXXX -> native AutoIt string (UCS-2LE).

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...