-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpers.au3
45 lines (40 loc) · 1.33 KB
/
helpers.au3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include-once
#include <StringConstants.au3>
;http://php.net/manual/en/function.str-replace.php
Func PHP_str_replace($search, $replace, $subject)
Local $replacements = 0, $i, $j
Local $Tsearch = IsArray($search)
$search = ArrayWrap($search)
Local $Treplace = IsArray($replace)
$replace = ArrayWrap($replace)
If (Not $Tsearch) And $Treplace Then Dim $replace = ["Array"]
If $Tsearch And (Not $Treplace) Then
ReDim $replace[UBound($search)]
For $i=1 To UBound($replace)-1
$replace[$i]=$replace[0]
Next
EndIf
If UBound($search) > UBound($replace) Then
Local $Ireplace = UBound($replace)
ReDim $replace[UBound($search)]
For $i=$Ireplace To UBound($replace)-1
$replace[$i] = ""
Next
EndIf
Local $Tsubject = IsArray($subject)
$subject = ArrayWrap($subject)
For $i=0 To UBound($subject)-1
If Not IsString($subject[$i]) Then Return SetError(1, $replacements, $Tsubject?$subject:$subject[0])
For $j=0 To UBound($search)-1
$subject[$i] = StringReplace($subject[$i], $search[$j], $replace[$j], 0, $STR_CASESENSE)
$replacements+=@extended
If @error<>0 Then Return SetError(@error, $replacements, $Tsubject?$subject:$subject[0])
Next
Next
Return SetError(0, $replacements, $Tsubject?$subject:$subject[0])
EndFunc
Func ArrayWrap($value)
If IsArray($value) Then Return $value
Local $return = [$value]
Return $return
EndFunc