Архив

Архив раздела ‘PHP’
5 декабря 2013 Нет комментариев
require "FGetCSV.php";
$content=file_get_contents($f);
if(($handle=fopen("php://memory","r+"))!==FALSE){
	fputs($handle,$content);
	rewind($handle);
	while($data=File_FGetCSV::fgetcsv($handle,65536,";")) {
		print_r($data);
	}
}

Скачать: FGetCSV
http://forum.dklab.ru/viewtopic.php?p=200945

Categories: PHP Tags:
5 декабря 2013 Нет комментариев
$abc=array();
$range=range(192,223);
foreach($range as $letter) {
	$abc[]=chr($letter);
}
print_r($abc);

http://www.kobzarev.com/programming/print-cyrillic-alphabet.html

Categories: PHP Tags:
5 декабря 2013 Нет комментариев
$abc=array();
foreach(range(chr(0xC0),chr(0xDF)) as $letter) {
	$abc[]=iconv('CP1251','UTF-8',$letter);
}
print_r($abc);

http://www.kobzarev.com/programming/print-cyrillic-alphabet.html

Categories: PHP Tags:

javascript:

$(document).ready(function(){
	var session;
	var secpic=document.getElementById('secpic');
	$.ajaxSetup({cache:false})
	$.get('gs.php',{requested:'captcha'},function (data) {
		session=data;
		secpic.value=$.trim(session);
	});
});

php:

session_start();
if (isset($_GET['requested'])) {
	print $_SESSION[$_GET['requested']];
}
else {
	print json_encode($_SESSION);
}

html:

<input type="hidden" id="secpic" name="secpic" value=""/>

http://stackoverflow.com/questions/2765175/how-to-get-session-variables-from-php-server-with-ajax-function-php-html-js-aj

Categories: Javascript, PHP Tags: ,
22 марта 2013 Нет комментариев

Отправляя

header("HTTP/1.0 404 Not Found");
exit();

в .htaccess

ErrorDocument 404 /404.php

работать не будет.
Можно отправлять заголовок

header("Location:/404.php");

но тогда ответ будет HTTP/1.1 302 Moved Temporarily

Решение: содержимое файла 404.php переносим в функцию

function Print404() {
	echo '<h1>404</h1><a href="/">back</a>';
}

и в коде:

header("HTTP/1.0 404 Not Found");
Print404();
exit();
Categories: PHP Tags:
17 марта 2013 1 комментарий
echo 'http://'.$_SERVER['HTTP_HOST'].substr($_SERVER['REQUEST_URI'],0,strpos($_SERVER['REQUEST_URI'],'?'));
Categories: PHP Tags:
10 марта 2013 Нет комментариев

В примере нужно собрать массив sql-запросов из файла дампа sql.

<?php
function remove_comments(&$output) {
	$lines = explode("\n", $output);
	$output = "";
	$linecount = count($lines);
	$in_comment = false;
	for($i = 0; $i < $linecount; $i++) {
		if( preg_match("/^\/\*/", preg_quote($lines[$i])) ) {
			$in_comment = true;
		}
		if( !$in_comment ) {
			$output .= $lines[$i] . "\n";
		}
		if( preg_match("/\*\/$/", preg_quote($lines[$i])) ) {
			$in_comment = false;
		}
	}
	unset($lines);
	return $output;
}
function remove_remarks($sql) {
	$lines = explode("\n", $sql);
	$sql = "";
	$linecount = count($lines);
	$output = "";
	for ($i = 0; $i < $linecount; $i++) {
		if (($i != ($linecount - 1)) || (strlen($lines[$i]) > 0)) {
			if ($lines[$i][0] != "#") {
				$output .= $lines[$i] . "\n";
			}
			else {
				$output .= "\n";
			}
			$lines[$i] = "";
		}
	}
	return $output;
}
function split_sql_file($sql, $delimiter) {
	$tokens = explode($delimiter, $sql);
	$sql = "";
	$output = array();
	$matches = array();
	$token_count = count($tokens);
	for ($i = 0; $i < $token_count; $i++) {
		if (($i != ($token_count - 1)) || (strlen($tokens[$i] > 0))) {
			$total_quotes = preg_match_all("/'/", $tokens[$i], $matches);
			$escaped_quotes = preg_match_all("/(?<!\\\\)(\\\\\\\\)*\\\\'/", $tokens[$i], $matches);
			$unescaped_quotes = $total_quotes - $escaped_quotes;
			if (($unescaped_quotes % 2) == 0) {
				$output[] = $tokens[$i];
				$tokens[$i] = "";
			}
			else {
				$temp = $tokens[$i] . $delimiter;
				$tokens[$i] = "";
				$complete_stmt = false;
				for ($j = $i + 1; (!$complete_stmt && ($j < $token_count)); $j++) {
					$total_quotes = preg_match_all("/'/", $tokens[$j], $matches);
					$escaped_quotes = preg_match_all("/(?<!\\\\)(\\\\\\\\)*\\\\'/", $tokens[$j], $matches);
					$unescaped_quotes = $total_quotes - $escaped_quotes;
					if (($unescaped_quotes % 2) == 1) {
						$output[] = $temp . $tokens[$j];
						$tokens[$j] = "";
						$temp = "";
						$complete_stmt = true;
						$i = $j;
					}
					else {
						$temp .= $tokens[$j] . $delimiter;
						$tokens[$j] = "";
					}
				}
			}
		}
	}
	return $output;
}
?>

Использование, учитывая что $ungzdata — текст из файла sql-дампа.

$SQL=array();
$SQL=remove_comments($ungzdata);
$SQL=remove_remarks($SQL);
$SQL=split_sql_file($SQL,";");
Categories: MySQL, PHP Tags: ,