I use the below block of code to validate Unique username.
function validateRepositoryUnique($field, $list, &$valid) {
if ( preg_grep('/^'.preg_quote($field->value).'$/i', $list) == -1) {
$valid = false;
$field->valid = false;
$field->error = '"' . $field->value . '" already exists.';
return false;
}
return true;
}
Example.
$filed->value = "test";
$list = array('test','test1','Test');
However I passed "test" in $filed->value. the Boolean kept showing value bool(true) when i did var_dump(validateRepositoryUnique($field, $list, &$valid));
And whatever I have inputted "test", "abc", "a", the Boolean kept return value bool(true).
My intention is when text found in array, it will return the $valid's value to false and print out the error.
Apology for my bad English and my basic knowledge of PHP programming language.
preg_grep does not return -1 if it finds no results. If returns an array of what it found. You can see the output in the example below.
Notice that I somewhat rewrote your function.
function validateRepositoryUnique($field, $list, &$valid) {
$preg = preg_grep('/^'.preg_quote($field->value).'$/i', $list) ;
var_dump($preg);
echo "\n";
if ( count($preg) == 0 ) {
$valid = false;
$field->valid = false;
$field->error = '"' . $field->value . '" already exists.';
return false;
}
return true;
}
$v;
$list = ['square', 'round', 'long'];
$f1 = new stdclass;
$f1->value = 'round';
$result = validateRepositoryUnique($f1, $list, $v);
var_dump($result);
echo "\n";
$f1->value = 'grass';
$result = validateRepositoryUnique($f1, $list, $v);
var_dump($result);
echo "\n";
$f1->value = 'triangle';
$result = validateRepositoryUnique($f1, $list, $v);
var_dump($result);
echo "\n";
Related
I want to make a method that returns keys and values. But only if the keys include the following string "_1" and "__last".
If only one matches then exit the function, only if the two string are included in the key, return the key with the value for a weather.
$infoList = array("_key_1"=>array("time"=>9, "day"=>"Tuesday", "weather"=>"sunny",
"humidity"=>"80%"),
"_key_2"=>array("time"=>5, "day"=>"Tuesday", "weather"=>"cloudy"),
"_key__last"=>array("time"=>3, "day"=>"Sunday", "weather"=>"rainy"))
public function getData() {
$list = array();
foreach($infoList as $key){
if(preg_match('/(_key)_(_1)/', $key) && preg_match('/(_key)_(__last)/', $key) == TRUE){
$list[$key] = $list[$key]["weather"]
}
}
return $list
}
You are making your life so much more difficult that it need be, use str_contains() its easier than building complex REGEX's and getting very confused by the look of it :)
I also fixed a number of other mistakes, such as the foreach that was not going to work, so check all the code.
It is also better to pass data to a function/method otherwise you get into scoping issues!
$infoList = array("_key_1"=>array("time"=>9, "day"=>"Tuesday", "weather"=>"sunny", "humidity"=>"80%"),
"_key_2"=>array("time"=>5, "day"=>"Tuesday", "weather"=>"cloudy"),
"_key__last"=>array("time"=>3, "day"=>"Sunday", "weather"=>"rainy"));
function getData(Array $infoList) {
$list = [];
$found = 0;
foreach($infoList as $key => $val) {
if( str_contains($key, '_1') || str_contains($key, '__last') ) {
$list[$key] = $val["weather"];
$found++;
}
}
if ( $found >= 2 ) {
return $list;
} else {
return false;
}
}
$res = getData($infoList);
if ( $res !== false ){
print_r($res);
} else {
echo 'Not Found';
}
RESULTS
Array
(
[_key_1] => sunny
[_key__last] => rainy
)
If you want to stick with RegEx, you can use positive lookaheads, the same way you check for passwords characters :
<?php
$pattern = '/^(?=.*_1)(?=.*_last).*$/';
$shouldMatch = [
'_1_last',
'foo_1bar_lasthello',
'_last_1',
'foo_lastbar_1hello'
];
echo 'next ones should match : ' . PHP_EOL;
foreach ($shouldMatch as $item)
{
if (preg_match($pattern, $item))
echo $item . PHP_EOL;
}
$shouldNOTMatch = [
'_2_first',
'bar_lasthello',
'foo_las_1hello'
];
echo 'next ones should NOT match : ' . PHP_EOL;
foreach ($shouldNOTMatch as $item)
{
// v------------ check
if (!preg_match($pattern, $item))
echo $item . PHP_EOL;
}
Output :
next ones should match :
_1_last
foo_1bar_lasthello
_last_1
foo_lastbar_1hello
next ones should NOT match :
_2_first
bar_lasthello
foo_las_1hello
I'm trying to write a function for arrays that strips the backslashes.
My problem is that I'm using the "is_array" function to verify if the input actually is an array but it seems that it executes numbers and strings as well.
The stripping part seems to work though, so it's the is_array part I can't figure out
I'm using the "array_map" function because the input array could consists of several arrays
What am I missing?
Thanks.
function strip_backslashes_array( $arr )
{
$result = is_array($arr) === true ?
array_map('strip_backslashes_array', $arr) :
implode( "",explode("\\",$arr ));
return $result;
}
$number = 5;
$text = 'Hey th\\\ere!';
$array = array("\\1","2\\",3,4);
$value1 = strip_backslashes_array($number);
echo $value1;
//returns 5. I would expect it to be Null as it is NOT an array.
$value2 = strip_backslashes_array($text);
echo $value2;
//returns "Hey there!" so it has stripped the string but I would expect Null as it is NOT an array.
echo '</br>';
$value3 = sanitize_strip_backslashes_array($array);
print_r($value3);
//returns "Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 )" so it has stripped slashes as expected because it is an array.
This function will strip backslashes from array and it will return null in case of other types if you give to the parameter strict true.
<?php
function strip_backslashes_array($arr, $strict = false){
$isarray = is_array($arr);
if ($strict && !$isarray) {
return null;
}
return $isarray? array_map('strip_backslashes_array', $arr) :
implode( "", explode("\\", $arr));
}
$number = 5;
$text = 'Hey th\\\ere!';
$array = array("\\1","2\\",3,4);
echo '</br>';
$value1 = strip_backslashes_array($number, true);
var_dump($value1);
echo '</br>';
$value2 = strip_backslashes_array($text, true);
var_dump($value2);
echo '</br>';
$value3 = strip_backslashes_array($array, true);
var_dump($value3);
Your recursion logic will not work if you want to return null when is_array($arr) == false. You wil need to use a different function to perform the string manipulation.
function strip_backslashes_array( $arr )
{
$result = is_array($arr) ?
array_map('strip_backslashes_str', $arr) :
null;
return $result;
}
function strip_backslashes_str($str){
if (is_array($str))
return strip_backslashes_array($str);
return implode( "", explode("\\", $str));
}
Thing for me is not clear. Before I just used preg_replace but on a newer version of php had to use preg_replace_callback after that happened to me this
PHP message: PHP Warning: preg_replace_callback(): Requires argument 2, '[$1_1]', to be a valid callback in
this &c=jQuery31102606949325169924_1480249921485?_=1484049921486
$this->aURI = explode('/', $_SERVER['SCRIPT_NAME']);
$sRequest = preg_replace('!'.$this->sURI.'(.*)$!i', '$1', $_SERVER['REQUEST_URI']);
if(substr($sRequest, -1)!='/')
$sRequest .= '/';
$sGets = $this->parseUrl($sRequest);
I dont know but i figure out when i insert url which has number 404 as value in any place I have a white page
private function parseUrl($sRequest){
$sVars = null;
foreach($this->aRoutingParse AS $k => $v){
if(!is_array($v))
continue;
preg_match_all('!\[(.+?)\]!i', $v[0], $aExpression_);
$sExpression = preg_replace_callback('!\[(.+?)\]!i', function($m) use ($k){
return $this->transformParam($m[1], $k);
}, $v[0]);
if(preg_match_all('!'.$sExpression.'!i', $sRequest, $aExpression__)){
foreach($aExpression__ AS $k_ => $v_){
foreach($v_ AS $kkk => $vvv){
if(!isset($aExpression_[1][$k_-1]))
$aExpression_[1][$k_-1] = null;
if($kkk>0)
$aExpression[] = array($aExpression_[1][$k_-1].'_'.$kkk, $vvv);
else
$aExpression[] = array($aExpression_[1][$k_-1], $vvv);
}
}
unset($aExpression[0]);
$iCount = count($aExpression__[0]);
if($iCount>1){
for($i=0;$i<$iCount;$i++){
if($i>0)
$sVars .= '&'.preg_replace_callback('!\[(.+?)\]!i', '[$1_'.$i.']', $v[1]);
else
$sVars = '&'.$v[1];
}
}else
$sVars = '&'.$v[1];
foreach($aExpression AS $k => $v_){
if(!isset($v['_'.$v_[0]]))
$v['_'.$v_[0]] = null;
if(!is_array($v['_'.$v_[0]]))
$sVars = str_replace('['.$v_[0].']', $v_[1], $sVars);
else {
$this->aRoutingParse = array($v['_'.$v_[0]]);
$sVars = $sVars.$this->parseUrl($v_[1]);
}
}
break;
}
}
return $sVars;
}
solution:
method return first the first found value
in $this->aRoutingParse i have this and somehow crash
array('404' => array('404', 'task=page&action=404'),
The preg_replace_callback expects a callback function as the second argument. You pass a string replacement pattern in preg_replace_callback('!\[(.+?)\]!i', '[$1_'.$i.']', $v[1]).
Instead preg_replace_callback('!\[(.+?)\]!i', '[$1_'.$i.']', $v[1]) use:
preg_replace('!\[(.+?)\]!i', '[$1_'.$i.']', $v[1])
I have this code:
$arr = array("Hello_backup","World!","Beautiful_backup","Day!");
if(in_array("backup", $arr)){
echo "Da";
} else { echo "Nu";
}
But is not working because,in_array instruction check the array for the complete string "backup" , which doesnt exist.I need to check for a part of the string,for example,to return true because backup is a part of the "Hello_backup" and "Beautiful_backup" strings
EDIT: I take the advice and i have used stripos like this:
$arr = array("Hello_backup-2014","World!","Beautiful_backup-2014","Day!");
$word='backup';
if(stripos($arr,$word) !== false){
echo "Da";
} else { echo "Nu";}
but now i get an error: "stripos() expects parameter 1 to be string, array given in if(stripos($arr,$word) !== false){"
Use implode to basically concatenate the array values as a string, then use strpos to check for a string within a string.
The first argument you pass to implode is used to separate each value in the array.
$array = array("Hello_backup","World!","Beautiful_backup","Day!");
$r = implode(" ", $array);
if (strpos($r, "backup") !== false) {
echo "found";
}
In this case you need to use stripos(). Example:
$arr = array("Hello_backup","World!","Beautiful_backup","Day!");
$needle = 'backup';
function check($haystack, $needle) {
foreach($haystack as $word) {
if(stripos($word, $needle) !== false) {
return 'Da!'; // if found
}
}
return 'Nu'; // if not found
}
var_dump(check($arr, $needle));
Without a function:
$arr = array("Hello_backup","World!","Beautiful_backup","Day!");
$found = false;
foreach($arr as $word) {
if(stripos($word, 'backup') !== false) {
$found = true;
break;
}
}
if($found) {
echo 'Da!';
} else {
echo 'Nu';
}
Try with strpos()
$arr = array("Hello_backup","World!","Beautiful_backup","Day!");
foreach($arr as $v){
echo (strpos($v,"backup")!== false ? "Da" : "Nu");
}
output :- DaNuDaNu
Here is the one line solution for you.
$arr = array("Hello_backup-2014","World!","Beautiful_backup-2014","Day!");
$returned_a = array_map(function($u){ if(stripos($u,'backup') !== false) return "Da"; else return "Nu";}, $arr);
You can use $returned_a with array as your answer..
Array ( [0] => Da [1] => Nu [2] => Da [3] => Nu )
Use this method. It is little bit simple to use.
$matches = preg_grep('/backup/', $arr);
$keys = array_keys($matches);
print_r($matches);
Look this working example
According to your question
$matches = preg_grep('/backup/', $arr);
$keys = array_keys($matches);
$matches = trim($matches);
if($matches != '')
{echo "Da";
}else { echo "Nu";}
<?php
$arr = array("Hello_backup","World!","Beautiful_backup","Day!");
foreach($arr as $arr1) {
if (strpos ($arr1,"backup")) {
echo "Da";
} else {
echo "Nu";
}
}
?>
I have the following in an INI file:
[country]
SE = Sweden
NO = Norway
FI = Finland
However, when var_dump()ing PHP's parse_ini_file() function, I get the following output:
PHP Warning: syntax error, unexpected BOOL_FALSE in test.ini on line 2
in /Users/andrew/sandbox/test.php on line 1
bool(false)
It appears that "NO" is reserved. Is there any other way I can set a variable named "NO"?
Another hack would be to reverse your ini keys with their values and use array_flip:
<?php
$ini =
"
[country]
Sweden = 'SE'
Norway = 'NO'
Finland = 'FI'
";
$countries = parse_ini_string($ini, true);
$countries = array_flip($countries["country"]);
echo $countries["NO"];
Still you will need to use quotes around NO (at least), if you do
Norway = NO
you don't get an error but value for $countries["NO"] will be an empty string.
This propably comes a little late but the way PHPs parse_ini_file works bothered me so much that I wrote my own little parser.
Feel free to use it, but use with care it has only been shallowly tested!
// the exception used by the parser
class IniParserException extends \Exception {
public function __construct($message, $code = 0, \Exception $previous = null) {
parent::__construct($message, $code, $previous);
}
public function __toString() {
return __CLASS__ . ": [{$this->code}]: {$this->message}\n";
}
}
// the parser
function my_parse_ini_file($filename, $processSections = false) {
$initext = file_get_contents($filename);
$ret = [];
$section = null;
$lineNum = 0;
$lines = explode("\n", str_replace("\r\n", "\n", $initext));
foreach($lines as $line) {
++$lineNum;
$line = trim(preg_replace('/[;#].*/', '', $line));
if(strlen($line) === 0) {
continue;
}
if($processSections && $line{0} === '[' && $line{strlen($line)-1} === ']') {
// section header
$section = trim(substr($line, 1, -1));
} else {
$eqIndex = strpos($line, '=');
if($eqIndex !== false) {
$key = trim(substr($line, 0, $eqIndex));
$matches = [];
preg_match('/(?<name>\w+)(?<index>\[\w*\])?/', $key, $matches);
if(!array_key_exists('name', $matches)) {
throw new IniParserException("Variable name must not be empty! In file \"$filename\" in line $lineNum.");
}
$keyName = $matches['name'];
if(array_key_exists('index', $matches)) {
$isArray = true;
$arrayIndex = trim($matches['index']);
if(strlen($arrayIndex) == 0) {
$arrayIndex = null;
}
} else {
$isArray = false;
$arrayIndex = null;
}
$value = trim(substr($line, $eqIndex+1));
if($value{0} === '"' && $value{strlen($value)-1} === '"') {
// too lazy to check for multiple closing " let's assume it's fine
$value = str_replace('\\"', '"', substr($value, 1, -1));
} else {
// special value
switch(strtolower($value)) {
case 'yes':
case 'true':
case 'on':
$value = true;
break;
case 'no':
case 'false':
case 'off':
$value = false;
break;
case 'null':
case 'none':
$value = null;
break;
default:
if(is_numeric($value)) {
$value = $value + 0; // make it an int/float
} else {
throw new IniParserException("\"$value\" is not a valid value! In file \"$filename\" in line $lineNum.");
}
}
}
if($section !== null) {
if($isArray) {
if(!array_key_exists($keyName, $ret[$section])) {
$ret[$section][$keyName] = [];
}
if($arrayIndex === null) {
$ret[$section][$keyName][] = $value;
} else {
$ret[$section][$keyName][$arrayIndex] = $value;
}
} else {
$ret[$section][$keyName] = $value;
}
} else {
if($isArray) {
if(!array_key_exists($keyName, $ret)) {
$ret[$keyName] = [];
}
if($arrayIndex === null) {
$ret[$keyName][] = $value;
} else {
$ret[$keyName][$arrayIndex] = $value;
}
} else {
$ret[$keyName] = $value;
}
}
}
}
}
return $ret;
}
What does it differently? Variable names may only consist of alphanumerical characters but other than that no restrictions to them. Strings must be encapsulated with " everything else has to be a special value like no, yes, true, false, on, off, null or none. For mapping see code.
Kind of a hack but you can add backticks around the key names:
[country]
`SE` = Sweden
`NO` = Norway
`FI` = Finland
Then access them like so:
$result = parse_ini_file('test.ini');
echo "{$result['`NO`']}\n";
Output:
$ php test.php
Norway
I was getting this error when there were single quote combinations in the string such as 't or 's. To get rid of the problem, I wrapped the string in double quotes:
Before:
You have selected 'Yes' but you haven't entered the date's flexibility
After:
"You have selected 'Yes' but you haven't entered the date's flexibility"
I ran into the same problem and tried to escape the name in every possible way.
Then I remembered that because of the INI syntax both names and values will be trimmed, so the following workaround MAYBE should do the trick:
NL = Netherlands
; A whitespace before the name
NO = Norway
PL = Poland
And it works ;) As long as your co-workers read the comments (which is not always the case) and don't delete it accidentally. So, yes, the array flipping solution is a safe bet.
From the manual page for parse_ini_file:
There are reserved words which must not be used as keys for ini files. These include: null, yes, no, true, false, on, off, none.
So no, you can't set a variable NO.