Архив

Архив раздела ‘PHP’
6 августа 2019 Нет комментариев

$max — максимальное кол-во переносов строк подряд

function remove_breaks($html,$max){
	//$html=trim(preg_replace("/[\r\n|\r|\n]{".($max+1).",}/u",str_repeat("\r\n",$max),$html));
	$html=trim(preg_replace("/(\r\n|\r|\n){".($max+1).",}/u",str_repeat("\r\n",$max),$html));
	return($html);
}
$html=remove_breaks($html,1);
Categories: PHP Tags:
6 августа 2019 Нет комментариев
function remove_comments($html){
	return preg_replace('/<!--(.*?)-->/','',$html);
}
Categories: PHP Tags:

Использовать javascript: window.top.location.href
Например вместо:

header("Location: ".$result_data->response->body->url_login);

указать:

header("Location: /?goto=".$result_data->response->body->url_login);

и на самой странице:

<?php
if(isset($_GET['goto'])){?>
<script>
	window.top.location.href="<?=$_GET['goto']?>";
	window.location.href="<?=$_GET['goto']?>";
</script>
<?}?>
Categories: Javascript, PHP Tags: ,
2 апреля 2019 Нет комментариев
header('X-XSS-Protection:0');
Categories: PHP Tags:
27 марта 2019 Нет комментариев

Например, для замены в $text {lists_4} на название списка и список элементов:

preg_match_all("/{lists\s*(.*?)}/si",$text,$matches,PREG_SET_ORDER);
if(count($matches)>0){
	foreach($matches as $m){
		$me=explode('_',trim(trim($m[0],'}'),"{"));
		if($me[0]=='lists'&&$me[1]>0){
			$replace='';
			$list=get_by_id($me[1],'lists');
			if($list['id']){
				$items=get_from_base('*','list_items',"`parent`='".$list['id']."' and `shown`=1",'pos');
				if(count($items)>0){
					$replace.='<div class="list">';
						$replace.='<div class="name">'.$list['name'].'</div>';
						$replace.='<ul>';
							foreach($items as $count=>$item){
								$replace.='<li class="item">'.$item['name'].'</li>';
							}
						$replace.='</ul>';
					$replace.='</div>';
				}
			}
			$text=str_replace($m,$replace,$text);
		}
	}
}
Categories: PHP Tags:
26 февраля 2019 Нет комментариев

JS:

$(".input_place").autocomplete({
	source:"/?ajax_action=get_places",
	minLength:4,
	position:{my:"left top",at:"left bottom"},
	select:function(event,ui){
		var loc=$(this).closest('form').attr('action');
		if(ui.item.country_id){
			loc+='?country[]='+ui.item.country_id;
			$('#hidden_country').val(ui.item.country_id);
		}
		if(ui.item.district_id){
			loc+='&district[]='+ui.item.district_id;
			$('#hidden_district').val(ui.item.district_id);
		}
		if(ui.item.city_id){
			loc+='&city[]='+ui.item.city_id;
			$('#hidden_city').val(ui.item.city_id);
		}
		var selected=ui.item.country_name;
		if(ui.item.district_name!=''){
			selected=ui.item.district_name;
		}
		if(ui.item.city_name!=''){
			selected=ui.item.city_name;
		}
		$(this).val(selected);
		if($('#hidden_country').val()==undefined){
			window.location.href=loc;
		}
		return false;
	}
}).autocomplete("instance")._renderItem=function(ul,item){
	var li=item.country_name;
	if(item.district_name!=''){
		li+=' '+item.district_name;
	}
	if(item.city_name!=''){
		li+=' '+item.city_name;
	}
	return $("<li>").append(li).appendTo(ul);
};
if($_GET['ajax_action']=='get_places'){
	$places=array();
	$ajax_countries=get_from_base('*','countries',"shown=1 and (`name` LIKE '%".$_GET['term']."%' OR `id` IN (SELECT `parent` FROM `districts` WHERE`shown`=1 and (`name` LIKE '%".$_GET['term']."%' OR `id` IN (SELECT `parent` FROM `cities` WHERE`shown`=1 and `name` LIKE '%".$_GET['term']."%'))))",'pos');
	if(count($ajax_countries)>0){
		foreach($ajax_countries as $country){
			$places[]=array('country_id'=>$country['id'],'country_name'=>$country['name'],'district_id'=>0,'district_name'=>'','city_id'=>0,'city_name'=>'');
			$ajax_districts=get_from_base('*','districts',"`parent`=".$country['id']." and `shown`=1 and (`name` LIKE '%".$_GET['term']."%' OR `id` IN (SELECT `parent` FROM `cities` WHERE`shown`=1 and `name` LIKE '%".$_GET['term']."%'))",'pos');
			if(count($ajax_districts)>0){
				foreach($ajax_districts as $district){
					$places[]=array('country_id'=>$country['id'],'country_name'=>$country['name'],'district_id'=>$district['id'],'district_name'=>$district['name'],'city_id'=>0,'city_name'=>'');
					$ajax_cities=get_from_base('*','cities',"`parent`=".$district['id']." and `shown`=1 and `name` LIKE '%".$_GET['term']."%'",'pos');
					if(count($ajax_cities)>0){
						foreach($ajax_cities as $city){
							$places[]=array('country_id'=>$country['id'],'country_name'=>$country['name'],'district_id'=>$district['id'],'district_name'=>$district['name'],'city_id'=>$city['id'],'city_name'=>$city['name']);
						}
					}
				}
			}
		}
	}
	header('Content-Type: application/json');
	echo json_encode($places);
	exit();
}

https://jqueryui.com/autocomplete/#custom-data

Categories: Javascript, PHP Tags: , ,
23 января 2019 Нет комментариев
if(count($_GET['f'])>0){
	reset($_GET['f']);
	$first_f=key($_GET['f']);
	echo $first_f;
}
Categories: PHP Tags: