55 lines
1.7 KiB
PHP
55 lines
1.7 KiB
PHP
<?php
|
|
|
|
namespace App\Paper;
|
|
|
|
|
|
class HtmlToPos
|
|
{
|
|
|
|
|
|
private function handleNewLine($text)
|
|
{
|
|
return str_replace(['<br>', '<br/>', '<br />'], "\n", $text);
|
|
}
|
|
|
|
public function convert($html = '')
|
|
{
|
|
$posText = $this->handleNewLine($html);
|
|
|
|
/**
|
|
* usuwa linijki które na początku mają ! albo #
|
|
*/
|
|
$posText = preg_replace('/^\s*[!#].*?$[\r\n]?/m', '', $posText);
|
|
|
|
|
|
/**
|
|
* poniższy kod wybieraz tekstu fragmenty w podwójnych nawiasach kwadratowych, rozdziela je po przecinkach
|
|
* następnie losuje element i podmienia go w miejsce oryginalnego fragmentu
|
|
*/
|
|
$re = "#\[\[(.*?)\]\]#s";
|
|
preg_match_all($re, $posText, $found, PREG_SET_ORDER, 0);
|
|
foreach ($found as $elements) {
|
|
$data = explode(',', $elements[1]);
|
|
$data = array_filter($data, function ($el) {
|
|
return strlen(trim($el));
|
|
});
|
|
$data = array_values($data);
|
|
$pos = strpos($posText, $elements[0]);
|
|
$posText = substr_replace($posText, $data[mt_rand(0, count($data) - 1)], $pos, strlen($elements[0]));
|
|
}
|
|
|
|
/**
|
|
* przeszukiwanie tekstu pod katem zawartości [ liczba, liczba2 ] i zamienianiu tego na randomową liczbę z przedziału
|
|
* liczba - liczba2
|
|
* obojętnie czy ujemną czy nie
|
|
*/
|
|
$re = '/\[\s*(-?\d+)\s*\,\s*(-?\d+)\s*\]/';
|
|
preg_match_all($re, $html, $matches, PREG_SET_ORDER, 0);
|
|
foreach ($matches as $match) {
|
|
$pos = strpos($posText, $match[0]);
|
|
$posText = substr_replace($posText, mt_rand($match[1], $match[2]), $pos, strlen($match[0]));
|
|
}
|
|
return $posText;
|
|
}
|
|
|
|
} |