-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmyfunc.php
executable file
·113 lines (102 loc) · 3.27 KB
/
myfunc.php
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
<?php
/************************************************************************
File: myfunc.php
License: Latest version of GNU Lesser General Protection License (LGPL)
which can be found at http://www.gnu.org
Author: Amos B. Batto (amosbatto@yahoo.com)
Created: 22 Jun 2006; Last Revised: 30 Jun 2006
This is my implementation of standard PHP functions which don't work the
way that I think that they should. There is a lot that I don't like about
PHP and I imagine that this file will grow in time.
*************************************************************************/
/* my_strpos() is my version of the standard strpos() function. It
is capable of taking arrays as its haystack and offset arguments.
If needle is an array, it searches for the first element of that array.
If haystack is an array, it searches within each array element. If the
needle isn't found, returns FALSE, otherwise it returns a two element
array. The first element is the position in the array where found and
the second is the position within the string. For example:
$a = array("this is OK", "but *big* is better");
$a = my_strpos("*big*", $a);
//now $a[0]=1, $a[1]=4 (found in array element 1 at string position 4)
If you want to start searching at different position in the array, set
strOffset and arrayOffset.*/
function myStrPos($haystack, $needle, $strOffset = 0, $arrayOffset = 0)
{
if (!is_string($needle))
{
if (is_array($needle))
$needle = $needle[0];
if (is_int($needle) || is_float($needle))
$needle = (string) $needle;
if (!$is_string($needle))
return false;
}
$aRet[0] = 0;
if (!is_array($haystack))
{
if (is_string($haystack) && $arrayOffset == 0)
{
$aRet[1] = strpos($haystack, $needle, $strOffset);
return ($aRet[1] === false ? false : $aRet);
}
else
return false;
}
for ($cnt = $arrayOffset; $cnt < count($haystack); $cnt++)
{
$aRet[1] = strpos($haystack[$cnt], $needle, ($arrayOffset == $cnt) ? $strOffset : 0 );
if (!($aRet[1] === false)) //if found
{
$aRet[0] = $cnt;
return $aRet;
}
}
return false;
}
//The following function doesn't work, because when called like this:
//my_implode(a_PoObj->aMsgid);
//It copies the aMsgid array to $a as an empty string. Really annoying!
//I don't understand why it does this
//argument that isn't an array. If passed any other variable type, it
//returns that type. This is useful in the ObjPo::match() function,
//which does type-sensitive comparisons, so you don't want to loose
//the variable type when comparing strings to Nulls.
function myImplode($a)
{
if (is_array($a))
return implode('', $a);
else
return $a;
}
//because realpath() doesn't work on directory names, created this function which
//can handle both file and directory names
function myRealPath($sFileOrDir)
{
if (!file_exists($sFileOrDir))
return false;
elseif (is_file($sFileOrDir))
return realpath($sFileOrDir);
else //only directories left now
{
$sCWD = getcwd();
chdir($sFileOrDir);
$sPath = getcwd();
chdir($sCWD);
return $sPath;
}
}
//In PHP 7.2 and later, count(NULL) throws a warning, so created a myCount() function
//to avoid the warning.
function myCount($a)
{
if (is_array($a))
return count($a);
elseif (is_string($a))
return 1;
elseif (is_null($a))
return 0;
else
return 1;
}
?>