$array = [
385 => 392,
386 => 392,
387 => 392,
390 => 402,
402 => 421,
405 => 401,
409 => 416,
410 => 401,
414 => 402,
416 => 389,
420 => 421,
421 => 422,
422 => 420,
];
foreach ($array as $key => $value) {
$level=0;
$leaf=[$key=>$level]; $v=$value;
while(true){
if(!isset($array[$v])) break;
$v=$array[$v];
if(isset($leaf[$v])) {
// loop found
// is it real loop ?
if($key==$v) {
printf("\n %s => %s", $key, $value);
}
break;
}
$leaf[$v]=++$level;
}
}
$list=CRest::call("crm.status.list",
[
'order'=>['SORT'=>'ASC'],
'filter'=>["ENTITY_ID"=>'INDUSTRY']
]
);
Array
(
[result] => Array
(
[0] => Array
(
[ID] => 69
[ENTITY_ID] => INDUSTRY
[STATUS_ID] => IT
[NAME] => Сельское хозяйство
[NAME_INIT] =>
[SORT] => 10
[SYSTEM] => N
[CATEGORY_ID] =>
[COLOR] =>
[SEMANTICS] =>
)
[1] => Array
(
[ID] => 71
[ENTITY_ID] => INDUSTRY
[STATUS_ID] => TELECOM
[NAME] => Добывающая
[NAME_INIT] =>
[SORT] => 20
[SYSTEM] => N
[CATEGORY_ID] =>
[COLOR] =>
[SEMANTICS] =>
)
...
<?php
$source = <<<'code'
<?php
$name = 'anon';
echo '<h2>';
echo 'Number is: ';
echo count(["1","2","3"]);
echo '</h2>';
echo 'CHECK
';
echo 1 + 3 . '-'. 4 . '=' . 0;
echo '
';
echo '<h2>';
echo 'Olololo';
echo '</h2>';
echo '
';
echo '<h3>';
echo 'QWErty';
echo '</h3>';
echo '
';
echo '<h2>';
echo 'Hello, ';
echo $name;
echo '</h2>';
echo '
';
foreach ([1,2,3] as $item) {
echo '
';
echo '<h2>';
echo 'Number is: ';
echo $item;
echo '</h2>';
echo '
';
}
code;
$tokens = token_get_all($source);
$last_token=false;
$getnext=function() use (&$tokens, &$last_token){
$token=array_shift($tokens);
if(is_null($token)) return false;
if(is_array($token)){
$last_token= token_name($token[0]);
return $token[1];
} else {
$last_token='';
return $token;
}
};
$getbracket=function($bracket) use (&$tokens,$getnext,&$last_token){
$stack=[]; $buf=$bracket;
if($bracket=='[') array_unshift($stack,']');
if($bracket=='(') array_unshift($stack,')');
while(false!==($x=$getnext())){
$buf.=$x;
if($x==$stack[0]){
array_shift($stack);
if(count($stack)==0) break;
}
if($x=='(') array_unshift($stack,')');
if($x=='{') array_unshift($stack,'}');
if($x=='[') array_unshift($stack,']');
}
return $buf;
};
$getecho=function() use (&$tokens,$getnext,$getbracket,&$last_token){
$buf='';
$buf2='';
$waitecho=false;
while(false!==($x=$getnext())){
//echo '{'.$last_token.' '.$x.'}';
if($last_token==='' && $x==';'){
$waitecho=true;
$buf2.=$x;
} elseif($last_token==='T_ECHO' && $waitecho){ // склеиваем
$waitecho=false;
$buf.=',';$buf2='';
} elseif($last_token==='T_WHITESPACE' && $waitecho){
$buf2.=$x;
} else if($waitecho){
return $buf.$buf2.$x;
} else if($last_token==='T_CONSTANT_ENCAPSED_STRING') {// склеиваем константные строки с одинаковыми кавычками
$s=$x[0];
$y = preg_replace('~'.preg_quote($s,"~'").'\s*[\.\,]\s*$~', '', $buf, 1, $count);
if($count>0)
$buf=$y.substr($x,1);
else
$buf.=$x;
} else if($last_token==='' && in_array($x,['[','('])) {
$buf.=$getbracket($x);
} else {
$buf.=$x;
}
};
return '';
};
while(false!==($x=$getnext())) {
echo $x;
if($last_token=='T_ECHO'){
echo $getecho();
}
}
?>
<?php
$name = 'anon';
echo '<h2>Number is: ', count(["1","2","3"]), '</h2>CHECK
', 1 + 3 . '-'. 4 . '=' . 0, '
<h2>Olololo</h2>
<h3>QWErty</h3>
<h2>Hello, ', $name, '</h2>
';
foreach ([1,2,3] as $item) {
echo '
<h2>Number is: ', $item, '</h2>
';
}
<?php
if (!isset($_SERVER['PHP_AUTH_USER'])) {
header('WWW-Authenticate: Basic realm="My Realm"');
header('HTTP/1.0 401 Unauthorized');
echo 'Authentification required.';
exit;
} else {
// config!
$password='test';
$user='test';
$upload_dir=$_SERVER['DOCUMENT_ROOT'].'/tmp/';
//
if($_SERVER['PHP_AUTH_USER']!=$user ||$_SERVER['PHP_AUTH_USER']!=$password){
echo 'No user found, sorry.';
exit;
}
$filename=$_SERVER['QUERY_STRING'];
if(!is_readable($upload_dir.$filename)){
echo 'No file found, sorry.';
exit;
}
header('Content-type: text/csv; charset=utf-8');
header('Content-Disposition: attachment; filename="' . $filename . '"');
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . filesize($upload_dir.$filename));
readfile($upload_dir.$filename);
}
count=2,<2;count=3,<300,>44
$current=45; // значение, которое сравниваем
$data=['count'=>3]; // внешние данные
if( ($data['count']==2 && $current<2) || ($data['count']==3 &&$current>44 && $current<300)){
// ...
}
function evaluate($current,$cond,$data=[]){
$res=false;
foreach(explode(';',$cond) as $or){ //or
foreach(explode(',',$or) as $and) { //and
if(preg_match('/^(.*?)(<=|>=|<|>|=)(.*?)$/',$and,$m)){
$a=trim($m[1]);$b=trim($m[3]);
if(empty($a)) $a=$current;
else if(is_numeric($a)) $a=0+$a;
else if(isset($data[$a])) $a=$data[$a];
else {
$res=false; break;
}
if(empty($b)) $b=$current;
else if(is_numeric($b)) $b=0+$b;
else if(isset($data[$b])) $b=$data[$b];
else {
$res=false; break;
}
if($m[2]=='<=') $res=$a<=$b;
else if($m[2]=='>=') $res=$a>=$b;
else if($m[2]=='>') $res=$a>$b;
else if($m[2]=='<') $res=$a<$b;
else if($m[2]=='=') $res=$a==$b;
} else {
$res=false;
//throw new \Exception('Некорректное условие в строке '.$and)
}
if(!$res) break; // выход по AND
}
if($res) break; // выход по OR
}
return $res;
}
$data=['count'=>3];
var_dump(true===evaluate(45,'count=2,<2;count=3,<300,>44', $data));
var_dump(true===evaluate(1,'<2'));
var_dump(true===evaluate(1,'>3;<2,>1;<2'));
$results_file='results.php';
$result=include_once($results_file);
$result[$current_user_id]=['post'=>$_POST, 'time'=>time()];
file_put_contents($results_file, '<'."?php\nreturn ".var_export($result,true).';')
<?php
return [];
if (preg_match('/\.gz$/', $name)) {
$_handle = fopen($name, "rb");
fseek($_handle, filesize($name) - 4);
$x = unpack("L", fread($_handle, 4));
$this->finish = $x[1];
fclose($_handle);
$handle = gzopen(
$handle, 'r'
);
} else {
$this->finish = filesize($name);
$handle = fopen($name, 'r');
}
$text=preg_replace('~\[CODE\](.*?)\[/CODE\]~si', '<pre>$1</pre>',$text);
$query=[];
parse_str(parse_url($query_string, PHP_URL_QUERY),$query_array);
unset($query_array['select']);
echo (!empty($query_array)?'?':'').http_build_query($query_array);
function getcontents($url){
if(filter_var($url,FILTER_VALIDATE_URL)) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_ENCODING, 'gzip');
// ssl?
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
$output = curl_exec($ch);
curl_close($ch);
return $output;
}
return file_get_contents($url);
}
echo getcontents('https://www.rusprofile.ru/id/11597949');
$text = 'JA Ja jA ja';
$text = preg_replace('/J(?i:a)/', 'Я', $text);
$text = preg_replace('/j(?i:a)/', 'я', $text);
curl_setopt($ch, CURLOPT_ENCODING ,"gzip")
str_replace(
array('january','february','march','april','may','june','july',
'august','september','october','november','december'),
array('января','февраля','марта','апреля','мая','июня','июля',
'августа','сентября','октября','ноября','декабря'),
strtolower(date('j F, Y г.',
$daystr)));
и локаль аглицкая, конечно...$re = '/((?:[-\s\(\)]*\d){9,})/m';
$str = 'D-24986 Satrup Tel. (0 46 33) 9 645 00 Fax (0 46 33) 9 66 000
Problemen: Tel. 03682-4654000,';
preg_match_all($re, $str, $matches, PREG_SET_ORDER, 0);
// Print the entire match result
var_dump($matches);