Baza modelek Glamour Models: Modelki, Fotomodelki, Hostessy

poniedziałek, 28 lutego 2022

Regex Laziness Instead of Greediness

Przykład użycia "Laziness Instead of Greediness": ".+?"
https://www.regular-expressions.info/repeat.html

<?php

$h='<ul>

<li>ps</li>
<li>ps</li>
<li class="klasa1 klasa2
klasa2
" >xyz</li>

<li>ps</li>
</ul>';

$pattern = '/(.*)(\<li.+?(klasa2).+?\<\/li\>)(.*)/s';
$replacement = '${1}<li>tu jest podmiana</li>${4}';
      
 
echo preg_replace($pattern, $replacement, $h);  


Wynik:

<ul>

<li>ps</li>
<li>ps</li>
<li>tu jest podmiana</li>

<li>ps</li>
</ul>

wtorek, 31 sierpnia 2021

SOLID Principles in Laravel: 5 Examples

sobota, 12 sierpnia 2017

Christmas tree without loops in PHP only 109 characters

113 chars:
$s=10;$r=range(1,$s);array_walk($r,function($v)use($s){echo str_repeat(' ',$s-$v).str_repeat('+',$v*2-1)."\n";});

109 chars:
$s=10;$r=range(1,$s);array_walk($r,function($v)use($s){printf('%'.($s+$v-1)."s\n",str_repeat('+',$v*2-1));});

125 chars:
$s=10;$r=range(1,$s);array_walk($r,function($v)use($s){echo str_pad(str_repeat('+',$v*2-1),$s+$v-1,' ',STR_PAD_LEFT)."\n";});


Result:
         +
        +++
       +++++
      +++++++
     +++++++++
    +++++++++++
   +++++++++++++
  +++++++++++++++
 +++++++++++++++++
+++++++++++++++++++

sobota, 1 kwietnia 2017

Jak dodać font do Photoshop bez instalacji w Windows

Jak dodać font do Photoshop bez instalacji w Windows?

Wystarczy wrzucić pliki czcionek do folderu
C:\Program Files (x86)\Common Files\Adobe\Fonts\
dla wersji 32-bit
lub
C:\Program Files\Common Files\Adobe\Fonts\
dla wersji 64-bit.

Jeśli katalog Fonts nie istnieje, to należy go utworzyć.

środa, 6 stycznia 2016

Jak stworzyć archiwum Zip w PHP

Przykład rekursywnego tworzenia archiwum Zip:

<?php

$directory = 'files';

$path = __DIR__ . '/' . $directory;
$filename  = sprintf('%s.zip', $path);


$zip = new ZipArchive;
$zip->open($filename, ZipArchive::CREATE);

$files = new RecursiveIteratorIterator(
    new RecursiveDirectoryIterator($path),
    RecursiveIteratorIterator::LEAVES_ONLY
);

foreach ($files as $name => $file) {
    if (! $file->isDir()) {
        $filePath = $file->getRealPath();
 
        $relativePath = substr($filePath, strlen($path) + 1);
 
        $zip->addFile($filePath, $relativePath);
    }
}

$zip->close();

środa, 31 grudnia 2014

Pobranie poprzedniego i następnego rekordu dowolnego zapytania w MySQL

Poniższe funkcje są przykładem, jak pobrać sąsiadujące wiersze w dowolnym zapytaniu z uwzględnieniem sortowania:

public function getPrevItem($id, $onlyActive = true, $categoryId = null) {
    $where = ' WHERE 1 = 1 ';
    $sqlParams = array();

    if ($onlyActive) {
        $where .= ' AND a.`active` = 1 AND c.`active` = 1 ';
    }

    if ($categoryId !== null) {
        $where .= ' AND a.id_category = ? ';
        $sqlParams[] = $categoryId;
    }

    $this->getSql()->query("SET @item_offset = -1");
    $sql = "SELECT * FROM (
                SELECT @item_offset := @item_offset + 1 AS `item_offset`, id
                FROM (
                    SELECT a.id
                    FROM  `articles` a
                    JOIN `articles_category` c ON c.id = a.id_category
                    $where
                    ORDER BY a.`pos` DESC, a.`id` DESC
                ) AS x
            ) AS t WHERE t.id = ?";

    $itemOffset = (int) $this->getSql()->value($sql, array_merge($sqlParams, array($id)));
    if ($itemOffset == 0) {
        return false;
    }

    $prevOffset = $itemOffset - 1;
    $sql = "SELECT a.*, c.name AS category_name, c.slug AS category_slug
            FROM `articles` a
            JOIN `articles_category` c ON c.id = a.id_category
            $where
            ORDER BY a.`pos` DESC, a.`id` DESC
            LIMIT 1
            OFFSET $prevOffset";

    $prev = $this->getSql()->one($sql);
    return $prev;
}

public function getNextItem($id, $onlyActive = true, $categoryId = null) {
    $where = ' WHERE 1 = 1 ';
    $sqlParams = array();

    if ($onlyActive) {
        $where .= ' AND a.`active` = 1 AND c.`active` = 1 ';
    }

    if ($categoryId !== null) {
        $where .= ' AND a.id_category = ? ';
        $sqlParams[] = $categoryId;
    }

    $this->getSql()->query("SET @item_offset = -1");
    $sql = "SELECT * FROM (
                SELECT @item_offset := @item_offset + 1 AS `item_offset`, id
                FROM (
                    SELECT a.id
                    FROM  `articles` a
                    JOIN `articles_category` c ON c.id = a.id_category
                    $where
                    ORDER BY a.`pos` DESC, a.`id` DESC
                ) AS x
            ) AS t WHERE t.id = ?";

    $itemOffset = (int) $this->getSql()->value($sql, array_merge($sqlParams, array($id)));

    $nextOffset = $itemOffset + 1;
    $sql = "SELECT a.*, c.name AS category_name, c.slug AS category_slug
            FROM `articles` a
            JOIN `articles_category` c ON c.id = a.id_category
            $where
            ORDER BY a.`pos` DESC, a.`id` DESC
            LIMIT 1
            OFFSET $nextOffset";

    $next = $this->getSql()->one($sql);
    return $next;
}


Legenda:
$this->getSql()->query() - wykonuje zapytanie
$this->getSql()->value() - zwraca wartość pierwszej kolumny w pierwszym rekordzie wyniku zapytania
$this->getSql()->one() - zwraca jeden rekord zapytania

wtorek, 13 sierpnia 2013

Ignorowanie polskich znaków w MySQL

Jeśli chcemy znaleźć słowo 'żółw', można wpisać tylko 'zolw' (opcja dla leniwych lub niewyedukowanych).
Rozwiązanie jest takie:

SELECT * 
FROM `animals` 
WHERE `name` COLLATE utf8_general_ci LIKE '%zolw%';

SELECT * 
FROM `animals` 
WHERE `name` COLLATE utf8_general_ci LIKE '%żółw%';

Oba zapytania są równoważne.