PHP String Comparison Not Working - php

I am having an issue with PHP String comparison. It never turns out to be 0. Below I am trying to get a setting in a cookie and compare it with a given key, and return the value of that cookie. The cookies looks like this console=1|userId=5. The value is always -180 or something. I echo them out and the are the same, and the lengths are the same. Could it be an encoding issue?
$parameters = explode('|', $_COOKIE[Cisco_Rewards::REWARDS_SETTINGS_COOKIE_NAME]);
for ($i = 0; $i < count($parameters); $i++) {
$parameter = explode('=', $parameters[$i]);
if(count($parameter) > 1) {
echo strcasecmp(strtolower(trim($parameter[0])), trim(strtolower($name)));
if(strcasecmp(strtolower(trim($parameter[0])), trim(strtolower($name))) == 0) {
return $parameter[1];
}
}
}

You're missing break; in for loop!
Yes, it could be encoding issue because function strcasecmp() works with Unicode only. Multi-byte characters such as UTF-8 or UTF-16 cannot be compared with strcasecmp().
Also, strcasecmp() is case-insensitive function so using strtolower() against its parameters doesn't change function's result (string "example" is same as "EXAMPLE"m "eXaMPlE", "ExamPlE", etc).
You should set default result value (like $res=false;) to be sure that result is set after loop.
You should replace for loop block with foreach loop like this one bellow
$parameters = explode('|', $_COOKIE[Cisco_Rewards::REWARDS_SETTINGS_COOKIE_NAME]);
// this will handle result
$res = false;
foreach ($parameters as $value) {
$param = explode('=', $value);
if(count($parameter) > 1) {
// I guess this line is just for testing result
echo "param1=".trim($param[0])."; param2=".trim($name)."; Result=".strcasecmp(trim($param[0]), trim($name)) . "<br />\n";
if(strcasecmp(trim($param[0]), trim($name))) {
$res=$param[1];
// you should break here
break;
}
}
}
// try to output result before (testing purpose only)
var_dump($res);
return $res;
But to make this solution easy you can use this function
function getParamValue($parameters, $key) {
$res = false;
$plist = explode('|', $parameters);
foreach ($plist as $pair) {
$element = explode('=', $pair);
if (trim($element[0]) == trim($key)) {
$res = trim($element[1]);
break;
}
}
return $res;
}
So if you have $parameters string like "console=1|userid=8159" and want to find value for $key string "userid", put it into this function and it will return your result or false if $key was not found in $parameters list.
Example
$parameters = "console=1|userid=8159";
$name = "userid";
echo getParamValue($parameters, $name);
Output
8159
Now you can write your code this way
$parameters = explode('|', $_COOKIE[Cisco_Rewards::REWARDS_SETTINGS_COOKIE_NAME]);
$value = getParamValue($parameters, $name);
And $value takes your result or returns false if $name is not in $parameters list.

Related

ExpressionEngine: Pass parameter into variable tag pair and fetch in the function

I need to pass parameter into inner tag, ex.
{exp:myplugin:func}
{last_year subject="xxx"}
{/last_year}
{/exp:myplugin:func}
How can I fetch that parameter in the function?
Here's a function I've written to return the parameters for a variable pair inside a template tag pair:
private function get_variable_parameters($tagData, $varName) {
$parameters = array();
if (strpos($tagData, LD."/".$varName.RD) !== FALSE) {
//### Closing variable tag exists ###
if (preg_match_all("/".LD.$varName."(.*?)".RD."(.*?)".LD."\/".$varName.RD."/s", $tagData, $matches)) {
for ($num = 0; $num < count($matches[0]); $num++){
$allParams = explode(" ", trim($matches[1][$num]));
foreach($allParams as $value) {
$value = str_replace(array('"', "'"), '', $value);
$param = explode("=", $value);
if (!empty($param[1]))
$parameters[$param[0]] = $param[1];
}
}
}
}
return $parameters;
}//### End of get_variable_parameters function ###
So from your example code in your func method:
$tagData = ee()->TMPL->tagdata;
$varParameters = $this->get_variable_parameters($tagData, "last_year");
echo $varParameters["subject"];
Looking back over my code though, I don't think it handles multiple use of the same variable pair inside the same loop, so if required may need to change this line:
$parameters[$param[0]] = $param[1];
to:
$parameters[$num][$param[0]] = $param[1];
And then work out what instance of the variable pairs inside the same loop. Untested though, probably needs more work!

PHP Function that can return value from an array key a dynamic number of levels deep

Using PHP, I would like to write a function that accomplishes what is shown by this pseudo code:
function return_value($input_string='array:subArray:arrayKey')
{
$segments = explode(':',$input_string);
$array_depth = count(segments) - 1;
//Now the bit I'm not sure about
//I need to dynamically generate X number of square brackets to get the value
//So that I'm left with the below:
return $array[$subArray][$arrayKey];
}
Is the above possible? I'd really appreciate some pointer on how to acheive it.
You can use a recursive function (or its iterative equivalent since it's tail recursion):
function return_value($array, $input_string) {
$segments = explode(':',$input_string);
// Can we go next step?
if (!array_key_exists($segments[0], $array)) {
return false; // cannot exist
}
// Yes, do so.
$nextlevel = $array[$segments[0]];
if (!is_array($nextlevel)) {
if (1 == count($segments)) {
// Found!
return $nextlevel;
}
// We can return $nextlevel, which is an array. Or an error.
return false;
}
array_shift($segments);
$nextsegments = implode(':', $segments);
// We can also use tail recursion here, enclosing the whole kit and kaboodle
// into a loop until $segments is empty.
return return_value($nextlevel, $nextsegments);
}
Passing one object
Let's say we want this to be an API and pass only a single string (please remember that HTTP has some method limitation in this, and you may need to POST the string instead of GET).
The string would need to contain both the array data and the "key" location. It's best if we send first the key and then the array:
function decodeJSONblob($input) {
// Step 1: extract the key address. We do this is a dirty way,
// exploiting the fact that a serialized array starts with
// a:<NUMBEROFITEMS>:{ and there will be no "{" in the key address.
$n = strpos($input, ':{');
$items = explode(':', substr($input, 0, $n));
// The last two items of $items will be "a" and "NUMBEROFITEMS"
$ni = array_pop($items);
if ("a" != ($a = array_pop($items))) {
die("Something strange at offset $n, expecting 'a', found {$a}");
}
$array = unserialize("a:{$ni}:".substr($input, $n+1));
while (!empty($items)) {
$key = array_shift($items);
if (!array_key_exists($key, $array)) {
// there is not this item in the array.
}
if (!is_array($array[$key])) {
// Error.
}
$array = $array[$key];
}
return $array;
}
$arr = array(
0 => array(
'hello' => array(
'joe','jack',
array('jill')
)));
print decodeJSONblob("0:hello:1:" . serialize($arr));
print decodeJSONblob("0:hello:2:0" . serialize($arr));
returns
jack
jill
while asking for 0:hello:2: would get you an array { 0: 'jill' }.
you could use recursion and array_key_exists to walk down to the level of said key.
function get_array_element($key, $array)
{
if(stripos(($key,':') !== FALSE) {
$currentKey = substr($key,0,stripos($key,':'));
$remainingKeys = substr($key,stripos($key,':')+1);
if(array_key_exists($currentKey,$array)) {
return ($remainingKeys,$array[$currentKey]);
}
else {
// handle error
return null;
}
}
elseif(array_key_exists($key,$array)) {
return $array[$key];
}
else {
//handle error
return null;
}
}
Use a recursive function like the following or a loop using references to array keys
<?php
function lookup($array,$lookup){
if(!is_array($lookup)){
$lookup=explode(":",$lookup);
}
$key = array_shift($lookup);
if(!isset($array[$key])){
//throw exception if key is not found so false values can also be looked up
throw new Exception("Key does not exist");
}else{
$val = $array[$key];
if(count($lookup)){
return lookup($val,$lookup);
}
return $val;
}
}
$config = array(
'db'=>array(
'host'=>'localhost',
'user'=>'user',
'pass'=>'pass'
),
'data'=>array(
'test1'=>'test1',
'test2'=>array(
'nested'=>'foo'
)
)
);
echo "Host: ".lookup($config,'db:host')."\n";
echo "User: ".lookup($config,'db:user')."\n";
echo "More levels: ".lookup($config,'data:test2:nested')."\n";
Output:
Host: localhost
User: user
More levels: foo

URI segment to associative array in code igniter

I have following url:
www.example.com/index.php/search/search_data/Doctor:a/Gender:Male/Language:Urdu/
and I want to convert it to associative array like
$data=array(
'Doctor'=> 'a',
'Gender'=> 'Male',
'Language'=> 'Urdu'
);
I have tried to do this using codeIgniter's URI class function
$this->uri->uri_to_assoc(n)
but as it accepts the data to be separated via '/' but I am having data with ':' as separator.
please help me.
I don't think there's an easier way to do this, rather than to do it manually.
First, retrieve the total segments, loop through, see if it contains ":", then add it into the array.
$segments = $this->uri->segment_array();
$search_array = array();
foreach($segments as $segment) {
if (strpos($segment, ":") !== FALSE) {
$e_array = explode(":", $segment);
$search_array[$e_array[0]] = $e_array[1];
}
}
Running that snippet somewhere will give you desirable results, $search_array will be an associative array with key => value.
You could hack the URI.php file. Change lines 431 - 439 to;
if (strpos($seg, ":") !== FALSE) {
list($parameter, $value) = explode(':', $seg);
if ($i % 2) {
$retval[$parameter] = $value;
} else {
$retval[$parameter] = $value;
$lastval = $seg;
}
} else {
if ($i % 2) {
$retval[$lastval] = $seg;
} else {
$retval[$seg] = FALSE;
$lastval = $seg;
}
}

PHP dynamically accessing variable value

I want to dynamically access value of variable, let's say I have this array:
$aData = array(
'test' => 123
);
Standard approach to print the test key value would be:
print $aData['test'];
However, if I have to work with string representation of variable (for dynamic purposes)
$sItem = '$aData[\'test\']';
how can I achieve to print aData key named test? Neither of examples provided below works
print $$sItem;
print eval($sItem);
What would be the solution?
Your eval example is lacking the return value:
print eval("return $sItem;");
should do it:
$aData['test'] = 'foo';
$sItem = '$aData[\'test\']';
print eval("return $sItem;"); # foo
But it's not recommended to use eval normally. You can go into hell's kitchen with it because eval is evil.
Instead just parse the string and return the value:
$aData['test'] = 'foo';
$sItem = '$aData[\'test\']';
$r = sscanf($sItem, '$%[a-zA-Z][\'%[a-zA-Z]\']', $vName, $vKey);
if ($r === 2)
{
$result = ${$vName}[$vKey];
}
else
{
$result = NULL;
}
print $result; # foo
This can be done with some other form of regular expression as well.
As your syntax is very close to PHP an actually a subset of it, there is some alternative you can do if you want to validate the input before using eval. The method is to check against PHP tokens and only allow a subset. This does not validate the string (e.g. syntax and if a variable is actually set) but makes it more strict:
function validate_tokens($str, array $valid)
{
$vchk = array_flip($valid);
$tokens = token_get_all(sprintf('<?php %s', $str));
array_shift($tokens);
foreach($tokens as $token)
if (!isset($vchk[$token])) return false;
return true;
}
You just give an array of valid tokens to that function. Those are the PHP tokens, in your case those are:
T_LNUMBER (305) (probably)
T_VARIABLE (309)
T_CONSTANT_ENCAPSED_STRING (315)
You then just can use it and it works with more complicated keys as well:
$aData['test'] = 'foo';
$aData['te\\\'[]st']['more'] = 'bar';
$sItem = '$aData[\'test\']';
$vValue = NULL;
if (validate_tokens($sItem, array(309, 315, '[', ']')))
{
$vValue = eval("return $sItem;");
}
I used this in another answer of the question reliably convert string containing PHP array info to array.
No eval necessary if you have (or can get) the array name and key into separate variables:
$aData = array(
'test' => 123
);
$arrayname = 'aData';
$keyname = 'test';
print ${$arrayname}[$keyname]; // 123
You can just use it like an ordinary array:
$key = "test";
print $aData[$key];
Likewise $aData could itself be an entry in a larger array store.
As alternative, extracting the potential array keys using a regex and traversing an anonymous array (should have mentioned that in your question, if) with references would be possible. See Set multi-dimensional array by key path from array values? and similar topics.
Personally I'm using a construct like this to utilize dynamic variable paths like varname[keyname] instead (similar to how PHP interprets GET parameters). It's just an eval in sheeps clothing (do not agree with the eval scaremongering though):
$val = preg_replace("/^(\w)+(\[(\w+)])$/e", '$\1["\3"]', "aData[test]");
The only solution in your case is to use Eval().
But please be very very very careful when doing this! Eval will evaluate (and execute) any argument you pass to it as PHP. So if you will feed it something that comes from users, then anyone could execute any PHP code on your server, which goes without saying is a security hole the size of Grand canyon!.
edit: you will have to put a "print" or "echo" inside your $sItem variable somehow. It will either have to be in $sItem ($sItem = 'echo $aData[\'test\']';) or you will have to write your Eval() like this: Eval ( 'echo ' . $sData ).
$sItem = '$aData[\'test\']';
eval('$someVar = '.$sItem.';');
echo $someVar;
Use eval() with high caution as others aldready explained.
You could use this method
function getRecursive($path, array $data) {
// transform "foo['bar']" and 'foo["bar"]' to "foo[bar]"
$path = preg_replace('#\[(?:"|\')(.+)(?:"|\')\]#Uis', '[\1]', $path);
// get root
$i = strpos($path, '[');
$rootKey = substr($path, 0, $i);
if (!isset($data[$rootKey])) {
return null;
}
$value = $data[$rootKey];
$length = strlen($path);
$currentKey = null;
for (; $i < $length; ++$i) {
$char = $path[$i];
switch ($char) {
case '[':
if ($currentKey !== null) {
throw new InvalidArgumentException(sprintf('Malformed path, unexpected "[" at position %u', $i));
}
$currentKey = '';
break;
case ']':
if ($currentKey === null) {
throw new InvalidArgumentException(sprintf('Malformed path, unexpected "]" at position %u', $i));
}
if (!isset($value[$currentKey])) {
return null;
}
$value = $value[$currentKey];
if (!is_array($value)) {
return $value;
}
$currentKey = null;
break;
default:
if ($currentKey === null) {
throw new InvalidArgumentException(sprintf('Malformed path, unexpected "%s" at position %u', $char, $i));
}
$currentKey .= $char;
break;
}
}
if ($currentKey !== null) {
throw new InvalidArgumentException('Malformed path, must be and with "]"');
}
return $value;
}

How to extract specific variables from a string?

let's say i have the following:
$vars="name=david&age=26&sport=soccer&birth=1984";
I want to turn this into real php variables but not everything. By example, the functions that i need :
$thename=getvar($vars,"name");
$theage=getvar($vars,"age");
$newvars=cleanup($vars,"name,age"); // Output $vars="name=david&age=26"
How can i get only the variables that i need . And how i clean up the $vars from the other variables if possible?
Thanks
I would use parse_str() and then manipulate the array.
$vars="name=david&age=26&sport=soccer&birth=1984";
parse_str($vars, $varray);
$thename = $varray["name"];
$theage = $varray["age"];
$newvars = array_intersect_key($varray,
array_flip(explode(",","name,age")));
You can do something like:
function getvar($arr,$key) {
// explode on &.
$temp1 = explode('&',$arr);
// iterate over each piece.
foreach($temp1 as $k => $v) {
// expolde again on =.
$temp2 = explode('=',$v);
// if you find key on LHS of = return wats on RHS.
if($temp2[0] == $key)
return $temp2[1];
}
// key not found..return empty string.
return '';
}
and
function cleanup($arr,$keys) {
// split the keys string on comma.
$key_arr = explode(',',$keys);
// initilize the new array.
$newarray = array();
// for each key..call getvar function.
foreach($key_arr as $key) {
$newarray[] = $key.'='.getvar($arr,$key);
}
// join with & and return.
return implode('&',$newarray);
}
Here is a working example.

Categories