Архив

Публикации с меткой ‘PHP’
16 ноября 2015 2 комментария
function find_emails($text){
	$emails=array();
	preg_match_all('/([a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,6})/',$text,$potential_emails,PREG_SET_ORDER);
	for($i=0;$i<count($potential_emails);$i++){
		$potential_email=$potential_emails[$i][0];
		if (filter_var($potential_email,FILTER_VALIDATE_EMAIL)){
			if (!in_array($potential_email,$emails)){
				$emails[]=$potential_email;
			}
		}
	}
	return $emails;
}
Categories: PHP Tags:
16 ноября 2015 Нет комментариев
function htmlizeEmails($text){
	preg_match_all('/([a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,6})/',$text,$potentialEmails,PREG_SET_ORDER);
	$potentialEmailsCount=count($potentialEmails);
	for($i=0;$i<$potentialEmailsCount;$i++){
		if(filter_var($potentialEmails[$i][0],FILTER_VALIDATE_EMAIL)){
			$text=str_replace($potentialEmails[$i][0],'<a href="mailto:'.$potentialEmails[$i][0].'">'.$potentialEmails[$i][0].'</a>',$text);
		}
	}
}

Использование:

$str="Send me an email to [email protected].";
echo htmlizeEmails($str);
//Echoes "Send me an email to <a href="mailto:[email protected]">[email protected]</a>."

http://stackoverflow.com/questions/9763606/detecting-emails-in-a-text

Categories: PHP Tags:
2 ноября 2015 1 комментарий
$LastModified_unix=1234567890;//time()
$LastModified=gmdate("D, d M Y H:i:s \G\M\T",$LastModified_unix);
$IfModifiedSince=false;
if(isset($_ENV['HTTP_IF_MODIFIED_SINCE'])){
	$IfModifiedSince=strtotime(substr($_ENV['HTTP_IF_MODIFIED_SINCE'],5));
}
if(isset($_SERVER['HTTP_IF_MODIFIED_SINCE'])){
	$IfModifiedSince=strtotime(substr($_SERVER['HTTP_IF_MODIFIED_SINCE'],5));
}
if($IfModifiedSince&&$IfModifiedSince>=$LastModified_unix){
	header($_SERVER['SERVER_PROTOCOL'].' 304 Not Modified');
	exit;
}
header('Last-Modified: '.$LastModified);
Categories: PHP Tags:
2 ноября 2015 Нет комментариев

Необходимо настроить 301-й редирект со всех страниц сайта с символами верхнего регистра в URL на аналогичные страницы с нижним регистром.
При этом редирект не должен затрагивать GET параметры после знака «?» в URL.

$url_get=urldecode($_SERVER['REQUEST_URI']);
if(strpos($url_get,'?')!==false){
	$a=explode("?",$url_get);
	$a[0]=mb_strtolower($a[0]);
	$newurl=$a[0]."?".$a[1];
}
else{
	$newurl=mb_strtolower($url_get);
}
if(urldecode($_SERVER['REQUEST_URI'])!=$newurl){
	header('Location: '.$newurl,true,301);
}
Categories: PHP Tags:
9 октября 2015 Нет комментариев

Например разделить строку по латинской X и русской Х.

$exp=preg_split("/(x|х)/",$str);
Categories: PHP Tags:
9 октября 2015 Нет комментариев
$arFilter=Array("IBLOCK_ID"=>1,"PROPERTY_AUTHOR"=>$AUTHOR_ID);
$res=CIBlockElement::GetList(Array(),$arFilter,Array());
echo $res;
Categories: CMS Tags: ,
20 сентября 2015 Нет комментариев
$text=preg_replace('/style=\\"[^\\"]*\\"/','',$text);

http://community.sitepoint.com/t/remove-inline-style-with-preg-replace/21743/2

Categories: PHP Tags: