30 мая 2022
Нет комментариев
echo preg_replace('#<a.*?>(.*?)</a>#i','\1',$text);
echo preg_replace('#<a.*?>(.*?)</a>#i','\1',$text);
function phone_format($phone){ $d=preg_replace('~\D+~','',$phone); if(strlen($d)>=10){ $d10=substr($d,-10,10); return '+7 ('.substr($d10,0,3).') '.substr($d10,3,3).'-'.substr($d10,6,2).'-'.substr($d10,8,2); } elseif(strlen($d)==7){ return '+7 (812) '.substr($d,0,3).'-'.substr($d,3,2).'-'.substr($d,5,2); } else{ return $phone; } }
При:
$cities_get=file_get_contents(LOS_URL.'ajax.php?type=cities');
Ошибка:
<br /> <b>Warning</b>: file_get_contents(): SSL operation failed with code 1. OpenSSL Error messages: error:14090086:SSL routines:ssl3_get_server_certificate:certificate verify failed in <b>/var/www/site.ru/index.php</b> on line <b>241</b><br /> <br /> <b>Warning</b>: file_get_contents(): Failed to enable crypto in <b>/var/www/site.ru/index.php</b> on line <b>241</b><br /> <br /> <b>Warning</b>: file_get_contents(https://site.su/ajax.php?type=cities): failed to open stream: operation failed in <b>/var/www/site.ru/index.php</b> on line <b>241</b><br />
Решение:
$arrContextOptions=array( "ssl"=>array( "verify_peer"=>false, "verify_peer_name"=>false, ), ); $cities_get=file_get_contents(LOS_URL.'ajax.php?type=cities',false,stream_context_create($arrContextOptions));
При ошибке
Access to XMLHttpRequest at 'https://site1.ru/api.php' from origin 'https://site2.ru' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
добавить на отдающий данные сайт (в примере site1.ru) заголовок
header('Access-Control-Allow-Origin: *');
Массив:
print_r($result_price);
Array ( [econom_mag] => 1800 [econom] => 510 [def] => 870 )
Ищем ключ минимального значения массива:
$min_key=array_keys($result_price,min($result_price)); print_r($min_key);
Array ( [0] => econom )
javascript:
function canUseWebp(){ let elem=document.createElement('canvas'); if(!!(elem.getContext&&elem.getContext('2d'))){ return elem.toDataURL('image/webp').indexOf('data:image/webp')==0; } return false; } window.onload=function(){ let images=document.querySelectorAll('[data-bg]'); for(let i=0;i<images.length;i++){ let image=images[i].getAttribute('data-bg'); images[i].style.backgroundImage='url('+image+')'; } let isitFirefox=window.navigator.userAgent.match(/Firefox\/([0-9]+)\./); let firefoxVer=isitFirefox?parseInt(isitFirefox[1]):0; if(canUseWebp()||firefoxVer>=65){ let imagesWebp=document.querySelectorAll('[data-bg-webp]'); for(let i=0;i<imagesWebp.length;i++){ let imageWebp=imagesWebp[i].getAttribute('data-bg-webp'); imagesWebp[i].style.backgroundImage='url('+imageWebp+')'; } } };
html:
<div class="item" style="background-image:url('/img/banners/banner2.webp');" data-bg="/img/banners/banner2.jpg" data-bg-webp="/img/banners/banner2.webp"></div>
пример программного вывода:
function attr_background_image($image,$dir,$add=''){ $html=''; $image_path=ROOT_HTTP.$dir.$image; $image_pathinfo=pathinfo($image_path); $ext=($image_pathinfo['extension']=='jpg')?'jpeg':$image_pathinfo['extension']; if(USE_WEBP==1&&$ext!='svg'&&file_exists(ROOT_DIR.$dir.$image_pathinfo['filename'].'.webp')){ $html.=' style="background-image:url(\''.ROOT_HTTP.$dir.$image_pathinfo['filename'].'.webp\');" data-bg="'.ROOT_HTTP.$dir.$image.'" data-bg-webp="'.ROOT_HTTP.$dir.$image_pathinfo['filename'].'.webp"'; } else{ $html.=' style="background-image:url(\''.ROOT_HTTP.$dir.$image.'\');'.$add.'"'; } return $html; } $block_bg=''; if($block['photo']||$block['bgcolor']){ if($block['photo']){ $bgcolor=''; if($block['bgcolor']){ $bgcolor.='background-color:'.$block['bgcolor'].';'; } $block_bg=attr_background_image($block["photo"],IMAGES_BLOCKS_DIR,$bgcolor); } elseif($block['bgcolor']){ $block_bg.=' style="'; $block_bg.='background-color:'.$block['bgcolor'].';'; $block_bg.='"'; } }
или
<div class="item"<?=attr_background_image($item["photo"],IMAGES_BANNERS_DIR)?>>
https://webinmind.ru/javascript/webp-to-background-image
function write_ini($array,$file){ $res=array(); foreach($array as $key=>$val){ if(is_array($val)){ $res[]="[$key]"; foreach($val as $skey=>$sval){ $res[]="$skey = ".(is_numeric($sval)?$sval:'"'.$sval.'"'); } } else{ $res[]="$key = ".(is_numeric($val)?$val:'"'.$val.'"'); } } safefilerewrite($file,implode("\r\n",$res)); } function safefilerewrite($fileName,$dataToSave){ if($fp=fopen($fileName,'w')){ $startTime=microtime(TRUE); do{ $canWrite=flock($fp,LOCK_EX); if(!$canWrite){ usleep(round(rand(0,100)*1000)); } } while((!$canWrite)and((microtime(TRUE)-$startTime)<5)); if($canWrite){ fwrite($fp,$dataToSave); flock($fp,LOCK_UN); } fclose($fp); } } $ini=parse_ini_file($_SERVER['DOCUMENT_ROOT']."/file.ini"); $ini['key']='value'; write_ini($ini,$_SERVER['DOCUMENT_ROOT']."/file.ini");