Архив

Архив раздела ‘PHP’
9 ноября 2017 Нет комментариев

Используются плагины: AMP for WP — Accelerated Mobile Pages for WordPress, WP-GeSHi-Highlight.
Синтаксис внутри pre подсвечиваться конечно не будет.
В файл functions.php активной темы:
</pre > заменить на </pre> т.е. без пробела

function echapcode($a){
	return "<pre $a[1]>".htmlspecialchars($a[2])."</pre >";
}
$texte=preg_replace_callback('#<pre (.*?)>(.*?)</pre >#ius','echapcode',$text);
add_filter('the_content','new_pre_content');
function new_pre_content($content){
	if(function_exists('ampforwp_is_amp_endpoint')&&ampforwp_is_amp_endpoint()){
		$content=preg_replace_callback('#<pre (.*?)>(.*?)</pre >#ius','echapcode',$content);
	}
	return $content;
}
Categories: CMS, PHP Tags: ,
4 октября 2017 Нет комментариев

Форматирование телефонов, указанных в произвольном формате.
Например, для преобразования телефона в ссылку tel:

function href_tel($phone,$text=''){
	if(!$text){
		$text=$phone;
	}
	$d=preg_replace('~\D+~','',$phone);
	if(strlen($d)>=10){
		return '<a href="tel:+7'.substr($d,-10,10).'">'.$text.'</a>';
	}
	else{
		return $phone;
	}
}

Преобразование к формату +7 (999) 123-45-67:

function phone_format($phone){
	$d=substr(preg_replace('~\D+~','',$phone),-10,10);
	if(strlen($d)>=10){
		return "+7 (".substr($d,0,3).") ".substr($d,3,3)."-".substr($d,6,2)."-".substr($d,8,2);
	}
	else{
		return $phone;
	}
}
Categories: PHP Tags:
24 августа 2017 Нет комментариев

Для открытия в браузере:

header("Content-type:application/pdf");
header("Content-Disposition:inline;filename='agreement.pdf'");

Для скачивания:

header("Content-type:application/pdf");
header("Content-Disposition:attachment;filename='agreement.pdf'");
Categories: PHP Tags:
8 августа 2017 Нет комментариев

При формировании XML:

$text=preg_replace('/[^\x{0009}\x{000a}\x{000d}\x{0020}-\x{D7FF}\x{E000}-\x{FFFD}]+/u',' ',$text);

Полная функция:

function prepare_to_xml($text){
	$text=preg_replace('/[^\x{0009}\x{000a}\x{000d}\x{0020}-\x{D7FF}\x{E000}-\x{FFFD}]+/u',' ',$text);
	$text=strip_tags($text);
	$text=htmlspecialchars($text);
	$text=str_replace("\t"," ",$text);
	$text=str_replace("&amp;nbsp;"," ",$text);
	$text=str_replace("&nbsp;"," ",$text);
	$text=preg_replace("/[\r\n]+/i","",$text);
	$text=preg_replace('/ {2,}/',' ',$text);
	$text=trim($text);
	return $text;
}

Ну или просто:

function utf8_for_xml($string){
	return preg_replace('/[^\x{0009}\x{000a}\x{000d}\x{0020}-\x{D7FF}\x{E000}-\x{FFFD}]+/u',' ',$string);
}
Categories: PHP Tags: ,
function remains_parse_timestamp($t=0){
	$day=floor($t/86400);
	$hour=($t/3600)%24;
	$min=($t/60)%60;
	return array('day'=>$day,'hour'=>$hour,'min'=>$min);
}
function remains($t){
	$res='';
	if(time()<$t){
		$arr=remains_parse_timestamp($t-time());
		if($arr['day']>0){
			$res.=$arr['day'].' дн. ';
		}
		if($arr['hour']>0||$res!=''){
			$res.=$arr['hour'].' ч. ';
		}
		if($arr['min']>0||$res!=''){
			$res.=$arr['min'].' мин.';
		}
	}
	return $res;
}
echo remains(1504389720);

В функцию передаем unix timestamp даты в будущем.

Categories: PHP Tags:

Например, чтобы преобразовать &#9766; в ☦

html_entity_decode($text,ENT_NOQUOTES,'UTF-8')

Реальный пример:

<meta name="description" content="<?=htmlspecialchars(html_entity_decode($description,ENT_NOQUOTES,'UTF-8'))?>"/>
Categories: PHP Tags:
function conv($l){
	return iconv("utf-8","windows-1251",$l);
}
$array=array('один','два','три');
$new_array=array_map("conv",$array);
Categories: PHP Tags: