Архив

Публикации с меткой ‘PHP’
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: ,
25 февраля 2013 Нет комментариев

В примере у input id в формате inp1 по порядку.

if($matchcount=preg_match_all('@value="([^"]*)"@',$email_content_text,$m)){
	for($i=0;$i<=$matchcount;$i++){
		$email_content_text=preg_replace('/<input id="inp'.$i.'"[^>]*>/is',$m[1][$i],$email_content_text);
	}
}

Обновлено: чтобы не обязательно нужно было указывать id у input, но первый атрибут должен быть value, т.е <input value="1" type="text"…

if($matchcount=preg_match_all('@value="([^"]*)"@',$email_content_text,$m)){
	for($i=0;$i<=$matchcount;$i++){
		$email_content_text=preg_replace('/<input value="'.$m[1][$i].'"[^>]*>/is',$m[1][$i],$email_content_text);
	}
}
Categories: PHP Tags:
25 февраля 2013 Нет комментариев
if(preg_match("@<td class='nobrdr deltd'>@", $email_content_text, $m)){
	$email_content_text = preg_replace("/<td class='nobrdr deltd'>.*?<a[^>]*>.*?<\/td>/is", "", $email_content_text);
}
Categories: PHP Tags:
25 февраля 2013 Нет комментариев
if(preg_match('@<td class="nobrdr">Видалити<\/td>@', $email_content_text, $m)){
	$email_content_text = preg_replace('/<td class="nobrdr">Видалити<\/td>/is', '', $email_content_text);
}
Categories: PHP Tags:
25 февраля 2013 Нет комментариев

Понадобилось при отправке заполненной формы на почту.
В примере подразумевается что в форме 1 input.

if(preg_match('@value="([^"]*)"@', $email_content_text, $m)){
	$email_content_text = preg_replace('/<input[^>]*>/is', $m[1], $email_content_text);
}
Categories: PHP Tags:
11 февраля 2013 Нет комментариев
if (!function_exists('mb_ucfirst') && function_exists('mb_substr')) { 
	function mb_ucfirst($string) {  
		$string = mb_ereg_replace("^[\ ]+","", $string);  
		$string = mb_strtoupper(mb_substr($string, 0, 1, "UTF-8"), "UTF-8").mb_substr($string, 1, mb_strlen($string), "UTF-8" );  
		return $string;  
	}
}
$text=mb_ucfirst($text);

Обновлено: https://krylov.org.ua/php-strtolower-krome-pervoj-bukvy-slovo-s-bolshoj/

Categories: PHP Tags:
11 февраля 2013 1 комментарий
$text =  preg_replace('/ {2,}/',' ',$text);
Categories: PHP Tags: