mein-sterntours/app/Services/Util.php
Kevin Adametz c8948338bb 01 2020
2020-02-14 10:18:20 +01:00

143 lines
No EOL
4.9 KiB
PHP

<?php
namespace App\Services;
class Util
{
public static function formatDate(){
if(\App::getLocale() == "en"){
return 'yyyy-mm-dd';
}
return 'dd.mm.yyyy';
}
public static function formatDateDB(){
if(\App::getLocale() == "en"){
return 'Y-m-d';
}
return 'd.m.Y';
}
public static function formatDateTimeDB(){
if(\App::getLocale() == "en"){
return 'Y-m-d - H:i';
}
return 'd.m.Y - H:i';
}
public static function _format_number($value){
return preg_replace("/[^0-9,]/", "", $value);
}
public static function _clean_float($value){
$groups = explode(".", preg_replace("/[^0-9.-]/", "", str_replace(',', '.', $value)));
$lastGroup = 0;
if(count($groups) > 1){
$lastGroup = array_pop($groups);
}
$number = implode('', $groups);
return (strlen($lastGroup) < 3) ? floatval($number.'.'.$lastGroup) : floatval($number.$lastGroup);
}
public static function sanitize($string, $force_lowercase = true, $anal = false, $substr = false)
{
$strip = array("~", "`", "!", "@", "#", "$", "%", "^", "&", "*", "(", ")", "_", "=", "+", "[", "{", "]",
"}", "\\", "|", ";", ":", "\"", "'", "&#8216;", "&#8217;", "&#8220;", "&#8221;", "&#8211;", "&#8212;",
"—", "–", ",", "<", ".", ">", "/", "?");
$clean = trim(str_replace($strip, "", strip_tags($string)));
$clean = preg_replace('/\s+/', "_", $clean);
$clean = ($anal) ? preg_replace("/[^a-zA-Z0-9]/", "", $clean) : $clean ;
if($substr){
$clean = (strlen($clean) > 33) ? substr($clean,-33) : $clean;
}
return ($force_lowercase) ?
(function_exists('mb_strtolower')) ?
mb_strtolower($clean, 'UTF-8') :
strtolower($clean) :
$clean;
}
public static function replacePlaceholders($search, $replace){
preg_match_all("/\{{(.+?)\}}/", $search, $matches);
if (isset($matches[1]) && count($matches[1]) > 0){
foreach ($matches[1] as $key => $value) {
$kvalue = trim($value);
if (array_key_exists($kvalue, $replace)){
$search = preg_replace("/\{\{$value\}\}/", $replace[$kvalue], $search);
}
}
}
return $search;
}
public static function cleanHTML($html)
{
$html = str_replace('font-size: 14px;', ' ', $html);
$html = str_replace('font-weight: lighter; ', ' ', $html);
$html = str_replace('font-family: Helvetica, Arial, sans-serif; ', ' ', $html);
$html = str_replace('font-family: Verdana, sans-serif; ', ' ', $html);
$html = str_replace('color: rgb(60, 60, 60); ', ' ', $html);
$html = str_replace(' style=" padding: 0px; margin-right: 0px; margin-left: 0px; margin-bottom: 20px !important; line-height: 20px !important;"', ' ', $html);
$html = str_replace('property="article"', ' ', $html);
$html = str_replace('&nbsp;', ' ', $html);
$html = str_replace('https://www.aegypten-online.de', 'https://www.sterntours.de', $html);
$dom = new \DOMDocument('1.0', 'utf-8');
@$dom->loadHTML(mb_convert_encoding($html, 'HTML-ENTITIES', 'UTF-8'));
$removeFullTags = ['span', 'a'];
foreach ($removeFullTags as $removeFullTag){
$domElemsToRemove = [];
$elements = $dom->getElementsByTagName($removeFullTag);
foreach ($elements as $element) {
$domElemsToRemove[] = $element;
}
foreach ($domElemsToRemove as $domElem) {
if($removeFullTag == 'span' && strpos($domElem->getAttribute('style'), 'font-weight: 700') !== false){
$new_node = $dom->createTextNode("<strong>".$domElem->nodeValue."</strong>");
}else{
$new_node = $dom->createTextNode($domElem->nodeValue);
}
$domElem->parentNode->replaceChild($new_node, $domElem);
}
}
$removeStyleTags = ['ul', 'li', 'h1', 'h2', 'br'];
foreach ($removeStyleTags as $removeStyleTag){
$elements = $dom->getElementsByTagName($removeStyleTag);
foreach ($elements as $element) {
$element->removeAttribute('style');
}
}
$html = $dom->saveHTML();
return $html;
}
public static function convertArrayWindowsCharset($values){
foreach ($values as $key=>$string){
$values[$key] = self::convertStringWindowsCharset($string);
}
return $values;
}
public static function convertStringWindowsCharset($value) {
$charset = mb_detect_encoding($value, "UTF-8, ISO-8859-1, ISO-8859-15", true);
return mb_convert_encoding($value, "Windows-1252", $charset);
}
}