Regexp and Arrays
am 02.01.2010 22:09:37 von Allen McCabe
--001485f854c6e58ca9047c34e740
Content-Type: text/plain; charset=ISO-8859-1
I have been plauged for a few days by this, can anyone see a problem with
this function??
function printByType($string, $mode)
{
(string) $string;
$lengths = array(
'VARCHAR' => 10
, 'TINYINT' => 1
, 'TEXT' => 10
, 'DATE' => 7
, 'SMALLINT' => 1
, 'MEDIUMINT' => 2
, 'INT' => 2
, 'BIGINT' => 3
, 'FLOAT' => 4
, 'DOUBLE' => 4
, 'DECIMAL' => 4
, 'DATETIME' => 10
, 'TIMESTAMP' => 10
, 'TIME' => 7
, 'YEAR' => 4
, 'CHAR' => 7
, 'TINYBLOB' => 10
, 'TINYTEXT' => 10
, 'BLOB' => 10
, 'MEDIUMBLOB' => 10
, 'MEDIUMTEXT' => 10
, 'LONGBLOB' => 10
, 'LONGTEXT' => 10
, 'ENUM' => 5
, 'SET' => 5
, 'BIT' => 2
, 'BOOL' => 1
, 'BINARY' => 10
, 'VARBINARY' => 10);
$types = array(
'VARCHAR' => 'text'
, 'TINYINT' => 'text'
, 'TEXT' => 'textarea'
, 'DATE' => 'text'
, 'SMALLINT' => 'text'
, 'MEDIUMINT' => 'text'
, 'INT' => 'text'
, 'BIGINT' => 'text'
, 'FLOAT' => 'text'
, 'DOUBLE' => 'text'
, 'DECIMAL' => 'text'
, 'DATETIME' => 'text'
, 'TIMESTAMP' => 'text'
, 'TIME' => 'text'
, 'YEAR' => 'text'
, 'CHAR' => 'text'
, 'TINYBLOB' => 'textarea'
, 'TINYTEXT' => 'textarea'
, 'BLOB' => 'textarea'
, 'MEDIUMBLOB' => 'textarea'
, 'MEDIUMTEXT' => 'textarea'
, 'LONGBLOB' => 'textarea'
, 'LONGTEXT' => 'textarea'
, 'ENUM' => 'text'
, 'SET' => 'text'
, 'BIT' => 'text'
, 'BOOL' => 'text'
, 'BINARY' => 'text'
, 'VARBINARY' => 'text');
switch ($mode)
{
case 'INPUT_LENGTH':
foreach ($lengths as $key => $val)
{
(string) $key;
(int) $val;
// DETERMINE LENGTH VALUE eg. int(6) GETS 6
preg_match('#\((.*?)\)#', $string, $match);
(int) $length_value = $match[1];
// SEARCH
$regex = "/" . strtolower($key) . "/i";
$found = preg_match($regex, $string);
if ($found !== false)
{
// DETERMINE ADD INTEGER eg. If the length_value is long enough,
determine number to increase html input length
switch ($length_value)
{
case ($length_value <= 7):
return $length_value;
break;
case ($length_value > 7 && $length_value < 15):
return $val += ($length_value/2);
break;
case ($length_value > 14 && $length_value < 101):
$result = ($length_value / 5);
$divide = ceil($result);
return $val += $divide;
break;
case ($length_value > 100):
return 40;
break;
default:
return 7;
break;
}
return $val;
}
else
{
return 7; // default value
}
}
break;
case 'INPUT_TYPE':
foreach ($types as $key => $val)
{
(string) $val;
(string) $key;
// SEARCH
$regex = "/" . strtolower($key) . "/i";
$found = preg_match($regex, $string);
if ($found === false)
{
return 'text'; // default value
}
else
{
return $val;
}
}
break;
}
} // END function printByType()
--001485f854c6e58ca9047c34e740--
Re: Regexp and Arrays
am 02.01.2010 22:19:46 von hSiplu
There can be a problem. But do you see a problem?? if yes. what is it?
May be we can find the solution.
--
Shiplu Mokaddim
My talks, http://talk.cmyweb.net
Follow me, http://twitter.com/shiplu
SUST Programmers, http://groups.google.com/group/p2psust
Innovation distinguishes bet ... ... (ask Steve Jobs the rest)
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: Regexp and Arrays
am 02.01.2010 22:25:21 von Mari Masuda
On a quick glance I don't think you are doing the casting correctly. =
For example, you have stuff like:
(string) $string;=20
and=20
(string) $key;
(int) $val;
and=20
(int) $length_value =3D $match[1];
and the casted value is not being saved anywhere.
I believe it should be something like $string =3D (string) $string; in =
order to assign the casted value back to the variable. In the third =
example, you probably meant $length_value =3D (int) $match[1];
On Jan 2, 2010, at 1:09 PM, Allen McCabe wrote:
> I have been plauged for a few days by this, can anyone see a problem =
with
> this function??
>=20
> function printByType($string, $mode)
> {
> (string) $string;
> $lengths =3D array(
> 'VARCHAR' =3D> 10
> , 'TINYINT' =3D> 1
> , 'TEXT' =3D> 10
> , 'DATE' =3D> 7
> , 'SMALLINT' =3D> 1
> , 'MEDIUMINT' =3D> 2
> , 'INT' =3D> 2
> , 'BIGINT' =3D> 3
> , 'FLOAT' =3D> 4
> , 'DOUBLE' =3D> 4
> , 'DECIMAL' =3D> 4
> , 'DATETIME' =3D> 10
> , 'TIMESTAMP' =3D> 10
> , 'TIME' =3D> 7
> , 'YEAR' =3D> 4
> , 'CHAR' =3D> 7
> , 'TINYBLOB' =3D> 10
> , 'TINYTEXT' =3D> 10
> , 'BLOB' =3D> 10
> , 'MEDIUMBLOB' =3D> 10
> , 'MEDIUMTEXT' =3D> 10
> , 'LONGBLOB' =3D> 10
> , 'LONGTEXT' =3D> 10
> , 'ENUM' =3D> 5
> , 'SET' =3D> 5
> , 'BIT' =3D> 2
> , 'BOOL' =3D> 1
> , 'BINARY' =3D> 10
> , 'VARBINARY' =3D> 10);
> $types =3D array(
> 'VARCHAR' =3D> 'text'
> , 'TINYINT' =3D> 'text'
> , 'TEXT' =3D> 'textarea'
> , 'DATE' =3D> 'text'
> , 'SMALLINT' =3D> 'text'
> , 'MEDIUMINT' =3D> 'text'
> , 'INT' =3D> 'text'
> , 'BIGINT' =3D> 'text'
> , 'FLOAT' =3D> 'text'
> , 'DOUBLE' =3D> 'text'
> , 'DECIMAL' =3D> 'text'
> , 'DATETIME' =3D> 'text'
> , 'TIMESTAMP' =3D> 'text'
> , 'TIME' =3D> 'text'
> , 'YEAR' =3D> 'text'
> , 'CHAR' =3D> 'text'
> , 'TINYBLOB' =3D> 'textarea'
> , 'TINYTEXT' =3D> 'textarea'
> , 'BLOB' =3D> 'textarea'
> , 'MEDIUMBLOB' =3D> 'textarea'
> , 'MEDIUMTEXT' =3D> 'textarea'
> , 'LONGBLOB' =3D> 'textarea'
> , 'LONGTEXT' =3D> 'textarea'
> , 'ENUM' =3D> 'text'
> , 'SET' =3D> 'text'
> , 'BIT' =3D> 'text'
> , 'BOOL' =3D> 'text'
> , 'BINARY' =3D> 'text'
> , 'VARBINARY' =3D> 'text');
>=20
> switch ($mode)
> {
> case 'INPUT_LENGTH':
> foreach ($lengths as $key =3D> $val)
> {
> (string) $key;
> (int) $val;
>=20
> // DETERMINE LENGTH VALUE eg. int(6) GETS 6
> preg_match('#\((.*?)\)#', $string, $match);
> (int) $length_value =3D $match[1];
>=20
> // SEARCH
> $regex =3D "/" . strtolower($key) . "/i";
> $found =3D preg_match($regex, $string);
>=20
> if ($found !== false)
> {
> // DETERMINE ADD INTEGER eg. If the length_value is long enough,
> determine number to increase html input length
> switch ($length_value)
> {
> case ($length_value <=3D 7):
> return $length_value;
> break;
> case ($length_value > 7 && $length_value < 15):
> return $val +=3D ($length_value/2);
> break;
> case ($length_value > 14 && $length_value < 101):
> $result =3D ($length_value / 5);
> $divide =3D ceil($result);
> return $val +=3D $divide;
> break;
> case ($length_value > 100):
> return 40;
> break;
> default:
> return 7;
> break;
> }
> return $val;
> }
> else
> {
> return 7; // default value
> }
> }
> break;
>=20
> case 'INPUT_TYPE':
>=20
> foreach ($types as $key =3D> $val)
> {
> (string) $val;
> (string) $key;
>=20
> // SEARCH
> $regex =3D "/" . strtolower($key) . "/i";
> $found =3D preg_match($regex, $string);
>=20
> if ($found ===3D false)
> {
> return 'text'; // default value
> }
> else
> {
> return $val;
> }
> }
> break;
> }
>=20
> } // END function printByType()
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: Regexp and Arrays
am 02.01.2010 22:53:29 von Allen McCabe
--000e0cd6e836c6e896047c3584d9
Content-Type: text/plain; charset=ISO-8859-1
I think part of the problem may lie in the use of variables in regular
expressions. I am trying to use the perl-style preg_match(), but the regular
expression values that it checks on each iteration of the foreach loop
checks for a different value (hence, the use of a variable).
On Sat, Jan 2, 2010 at 1:19 PM, shiplu wrote:
> There can be a problem. But do you see a problem?? if yes. what is it?
> May be we can find the solution.
>
> --
> Shiplu Mokaddim
> My talks, http://talk.cmyweb.net
> Follow me, http://twitter.com/shiplu
> SUST Programmers, http://groups.google.com/group/p2psust
> Innovation distinguishes bet ... ... (ask Steve Jobs the rest)
>
--000e0cd6e836c6e896047c3584d9--