function filter ($list) {
$newList = [];
foreach ($list as $line) {
$a = explode(' ', trim($line), 2);
if (count($a) > 0) {
$newList[] = $a[0];
}
}
return $newList;
}
$list = ['col', 'col(1)', 'col(1) string', 'col(1) string1 string2', 'col string1'];
print_r(filter($list));
php -r 'echo date("Y-m-d H:i:s", strtotime("second tuesday of this month 18:00"));'
2013-08-13 18:00:00
function getOrDef($val, $def = 'default')
{
return !empty($val) ? $val : $def;
}
$a = getOrDef($a, '234');
ErrorDocument 404 /404.php
<?php
header('Content-Type: text/plain; charset="UTF-8');
print_r($_SERVER);
Array
(
[REDIRECT_REQUEST_METHOD] => GET
[REDIRECT_STATUS] => 404
....
[HTTP_REFERER] => http://localhost/test.html
[REDIRECT_URL] => /test/dshgsdgsdhbshjdfh
[GATEWAY_INTERFACE] => CGI/1.1
[SERVER_PROTOCOL] => HTTP/1.1
[REQUEST_METHOD] => GET
[QUERY_STRING] =>
[REQUEST_URI] => /test/dshgsdgsdhbshjdfh
[SCRIPT_NAME] => /404.php
[PHP_SELF] => /404.php
[REQUEST_TIME_FLOAT] => 1375894390.798
[REQUEST_TIME] => 1375894390
[argv] => Array
(
)
[argc] => 0
)
function test_f($a) {
echo $a;
}
$f = 'test_f';
$f('Hello World');
header('Content-Type: text/plain; charset="UTF-8"');
echo "\$_FILES: ".print_r($_FILES, 1)."\n\n";
class TestException extends Exception{};
class A{
private $c;
public static $e = 'Hello World';
public function __construct(){
$this->c = new B();
}
public function test(){
return $this->c->d();
}
}
class B{
public function d(){
try{
throw new TestException();
}catch(TestException $e){
$trace = $e->getTrace();
$class = $trace[1]['class'];
$vars = get_class_vars($class);
return $vars['e'];
/*
либо:
return $class::$e;
если php >= 5.3
*/
}catch(Exception $e){
}
return null;
}
}
$a = new A();
var_dump( $a->test() );
// out: string(11) "Hello World"
$a = array(1,3,5,"c"=>5);
var_dump( range( 0, count($a) -1 ) == array_keys( $a ) );
define( 'STATE_QUOTE_OPEN', 1 );
define( 'STATE_QUOTE_CLOSE', 2 );
define( 'STATE_LQUOTE_OPEN', 3 );
define( 'STATE_LQUOTE_CLOSE', 4 );
define( 'STATE_BRACE_OPEN', 5 );
define( 'STATE_BRACE_CLOSE', 6 );
function parse( $str, $repl ){
$state = 0; // default
$buf = '';
$brace_pos_start = 0;
for( $i = 0; $i < strlen( $str ); $i++ ){
switch( $str[$i] ){
case '"':
$state = $state == STATE_QUOTE_OPEN ? STATE_QUOTE_CLOSE : STATE_QUOTE_OPEN;
break;
case "'":
$state = $state == STATE_LQUOTE_OPEN ? STATE_LQUOTE_CLOSE : STATE_LQUOTE_OPEN;
break;
case '{':
if( $state != STATE_QUOTE_OPEN && $state != STATE_LQUOTE_OPEN ){
$state = STATE_BRACE_OPEN;
$buf = '';
$brace_pos_start = $i;
}
break;
case '}':
if( $state == STATE_BRACE_OPEN ){
$state = STATE_BRACE_CLOSE;
$str = substr_replace( $str, $repl[ $buf ], $brace_pos_start, $i - $brace_pos_start + 1 );
$i = $brace_pos_start + strlen( $repl[ $buf ] );
}
break;
default:
if( $state == STATE_BRACE_OPEN ) $buf .= $str[$i];
break;
}
}
return $str;
}
echo parse( 'SELECT * FROM {table} WHERE name="123 {qwerty} 789" AND id IN (SELECT id FROM {other_table})',
array( 'table' => 'tbl1', 'other_table' => 'tbl2' ) );