Sign in to follow this  
Followers 0
JihanLukas

SOLVED PHP strtr like in autoit ?

12 posts in this topic

#1 ·  Posted (edited)

Someone knows a strtr (php) function in autoit ?

Thanks

Edited by JihanLukas

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

I don't knok php but from what I read looks like StringReplace() or can be done with StringReplace().

Edited by Andreik

When the words fail... music speaks

Share this post


Link to post
Share on other sites

I don't knok php but from what I read looks like StringReplace() or can be done with StringReplace().

Stringreplace replace one char with another, strtr replace chars in a string with a char of second string at same position :

strtr("Example", "xam", "Xzt) -> EXztple (x replaced with X, a replaced with z and m replaced with t)

Share this post


Link to post
Share on other sites

MsgBox(64, "I beg to differ :)", StringReplace("Example", "xam", "Xzt"))

Share this post


Link to post
Share on other sites

MsgBox(64, "I beg to differ :)", StringReplace("Example", "xame", "Xzty"))

Stringreplace replace one char with another, strtr replace chars in a string with a char of second string at same position :

strtr("One Example", "xame", "Xzty) -> Ony EXztply (all x replaced with X, all a replaced with z, all m replaced with t and all e with y)

'

string strtr ( string $str , string $from , string $to )

string strtr ( string $str , array $replace_pairs )

If given three arguments, this function returns a copy of str where all occurrences of each (single-byte) character in from have been translated to the corresponding character in to, i.e., every occurrence of $from[$n] has been replaced with $to[$n], where $n is a valid offset in both arguments.

If from and to have different lengths, the extra characters in the longer of the two are ignored. The length of str will be the same as the return value's.

'

Share this post


Link to post
Share on other sites

hi JihanLukas :huh2:,

Ok, firstly, Welcome to the forums ;)

Secondly, did you try running the code I posted above? What did you see?

Your perception of StringReplace's behavior is wrong. Read the help file and you will learn that it replaces a substring in a string, not just a single character.

-smartee

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

strtr("One Example", "xame", "Xzty) -> Ony EXztply (all x replaced with X, all a replaced with z, all m replaced with t and all e with y)

If from and to have different lengths, the extra characters in the longer of the two are ignored. The length of str will be the same as the return value's.

I did a function for you. Take a look to check whether this is what you need (or take it as an example to do your own function):

$string = "this is a Test"
$first = "tTieat"
$second = "Tt134"

;Example
$resultstring = _strtr($string, $first, $second)
ConsoleWrite("Origin: " & $string & @CRLF & "Result: " & $resultstring & @CRLF)


;Here starts the function
Func _strtr($yourstring, $param1, $param2)
    ;If any of the params are empty, set error and return
    If $param1 = "" Or $param2 = "" Or $yourstring = "" Then Return SetError(-1,-1,-1)

    ;Check params lenght
    $lenghtparam1 = StringLen($param1)
    $lenghtparam2 = StringLen($param2)

    ;Check both params an
    Select
        Case $lenghtparam1 < $lenghtparam2
            $param2 = StringLeft($param2,$lenghtparam1)
        Case $lenghtparam1 > $lenghtparam2
            $param1 = StringLeft($param1,$lenghtparam2)
    EndSelect

    ;Put all characters of the params and the string in arrays
    $aparam1 = StringSplit($param1,"")
    $aparam2 = StringSplit($param2,"")
    $ayourstring = StringSplit($yourstring,"")

    ;Loop the string and replace every character (if needed) ;). It is Case Sensitive
    For $i = 1 to $ayourstring[0]
        For $j = 1 to $aparam1[0]
            If $ayourstring[$i] == $aparam1[$j] Then
                $ayourstring[$i] = $aparam2[$j]
                ExitLoop
            EndIf
        Next
    Next

    ;Put replaced string array into a single string and return it
    Local $result = ""
    For $i = 1 to $ayourstring[0]
        $result &= $ayourstring[$i]
    Next
    Return $result
EndFunc ;==>_strtr

Edit: I forgot to cut $params to made them the same lenght. Script fixed.

Edit2: Added some comments and also a check to avoid an empty string.

Edited by sahsanu

Share this post


Link to post
Share on other sites

hi JihanLukas :huh2:,

Ok, firstly, Welcome to the forums ;)

Secondly, did you try running the code I posted above? What did you see?

Your perception of StringReplace's behavior is wrong. Read the help file and you will learn that it replaces a substring in a string, not just a single character.

-smartee

But i dont want to replace a substring... but chars !!!

Share this post


Link to post
Share on other sites

#9 ·  Posted (edited)

Thanks....

I tried to use RegEx to do it, but found no solution...

Your function works...

I did a function for you. Take a look to check whether this is what you need (or take it as an example to do your own function):

$string = "this is a Test"
$first = "tTieat"
$second = "Tt134"

;Example
$resultstring = _strtr($string, $first, $second)
ConsoleWrite("Origin: " & $string & @CRLF & "Result: " & $resultstring & @CRLF)


;Here starts the function
Func _strtr($yourstring, $param1, $param2)
    ;If any of the params are empty, set error and return
    If $param1 = "" Or $param2 = "" Or $yourstring = "" Then Return SetError(-1,-1,-1)

    ;Check params lenght
    $lenghtparam1 = StringLen($param1)
    $lenghtparam2 = StringLen($param2)

    ;Check both params an
    Select
        Case $lenghtparam1 < $lenghtparam2
            $param2 = StringLeft($param2,$lenghtparam1)
        Case $lenghtparam1 > $lenghtparam2
            $param1 = StringLeft($param1,$lenghtparam2)
    EndSelect

    ;Put all characters of the params and the string in arrays
    $aparam1 = StringSplit($param1,"")
    $aparam2 = StringSplit($param2,"")
    $ayourstring = StringSplit($yourstring,"")

    ;Loop the string and replace every character (if needed) ;). It is Case Sensitive
    For $i = 1 to $ayourstring[0]
        For $j = 1 to $aparam1[0]
            If $ayourstring[$i] == $aparam1[$j] Then
                $ayourstring[$i] = $aparam2[$j]
                ExitLoop
            EndIf
        Next
    Next

    ;Put replaced string array into a single string and return it
    Local $result = ""
    For $i = 1 to $ayourstring[0]
        $result &= $ayourstring[$i]
    Next
    Return $result
EndFunc ;==>_strtr

Edit: I forgot to cut $params to made them the same lenght. Script fixed.

Edit2: Added some comments and also a check to avoid an empty string.

Edited by JihanLukas

Share this post


Link to post
Share on other sites

:huh2: Oh ok, well here's another way that returns the number of replacements too :ph34r:

Guess what ;) It uses StringReplace() :alien:

Func _ReplaceCharsInString($sString, $sChars, $sReplaceChars)
    Local $iStringLen = StringLen($sString)
    Local $iCharsLen = StringLen($sChars)
    Local $iReplaceCharsLen = StringLen($sReplaceChars)
    Local $iReplacementsCount = 0
    If (($iStringLen + $iCharsLen) < 2) Then Return SetError(0, $iReplacementsCount, $sString)
    If $iCharsLen > $iReplaceCharsLen Then $sChars = StringLeft($sChars, $iReplaceCharsLen)
    For $i = 1 To StringLen($sChars)
        $sString = StringReplace($sString, StringMid($sChars, $i, 1), StringMid($sReplaceChars, $i, 1))
        $iReplacementsCount += @extended
    Next
    Return SetError(0, $iReplacementsCount, $sString)
EndFunc   ;==>_ReplaceCharsInString

MsgBox(64, "Demo 2", "_ReplaceCharsInString(""Example"", ""xame"", ""Xzty"")" & @CRLF & "Example=>" & _ReplaceCharsInString("Example", "xame", "Xzty") & @CRLF & "Number of replacements: " & @extended)

-smartee

Share this post


Link to post
Share on other sites

:huh2: Oh ok, well here's another way that returns the number of replacements too ;)

Hello smartee,

Just an issue with your function. It will replace characters over the modified string so it will return inaccurate results:

Following this example;

$string = "this is a Test"
$first = "tTieat"
$second = "Tt134"

$result = _ReplaceCharsInString($string, $first, $second)

It will return th1s 1s 4 t3st and it should be Th1s 1s 4 t3sT

Share this post


Link to post
Share on other sites
;) I thought that was the behavior desired, well then i guess your function will be the way to go :huh2:

Share this post


Link to post
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
Sign in to follow this  
Followers 0