function create_word(string $str, int $cnt): string
{
$len = \strlen($str);
if ($len === 0 || $cnt === 0) {
return '';
}
$result = '';
while ($cnt > $len) {
$i = ($cnt % ($len)) ?: $len;
$result = $str[$i - 1] . $result;
$cnt = ($cnt - $i) / $len;
}
$result = $str[$cnt - 1] . $result;
return $result;
}
echo create_word('abc', 9); //'bc'
echo create_word('abc', 18); //'abc'
function get_text($el) {
$text = "";
if ($el instanceof PhpOffice\PhpWord\Element\TextRun) {
foreach ($el->getElements() as $child) {
$text .= get_text($child);
}
} elseif ($el instanceof PhpOffice\PhpWord\Element\Text) {
$text .= $el->getText();
}
return $text;
}
class Db
{
private $pdo;
private static $instance;
private function __construct()
{
$dbOptions = (require __DIR__ . '/../../settings/setting.php')['db'];
$this->pdo = new PDO(
'mysql:host=' . $dbOptions['host'] . ';dbname=' . $dbOptions['dbname'],
$dbOptions['user'],
$dbOptions['password']
);
$this->pdo->exec('SET NAMES UTF8');
}
public static function getInstance(): self
{
// если объект подключения не создан и равен NULL
if (self::$instance === null) {
// то создается новый объект класса Db;
self::$instance = new self();
}
// если подключение создано, просто возвращаем уже созданное подключение
return self::$instance;
}
}
Db::getInstance();
const insert = (str, indices, substr = ' ') =>
Array.prototype.reduce.call(
str,
(acc, n, i) => acc + (indices.includes(i) ? substr : '') + n,
''
);
// или
const insert = (str, indices, substr = ' ') => [...indices]
.sort((a, b) => b - a)
.reduce((acc, n) => (acc.splice(n, 0, substr), acc), [...str])
.join('');
// или
const insert = (str, indices, substr = ' ') => []
.concat(0, indices)
.sort((a, b) => a - b)
.map((n, i, a) => str.slice(n, a[i + 1]))
.join(substr);
// или
const insert = (str, indices, substr = ' ') => indices.length
? str.replace(RegExp(indices.map((n, i) => `(?<=^.{${n}})`).join('|'), 'g'), substr)
: str;
str = insert(str, [ 1, 3, 6, 8, 10 ]);
. // Если нужно только количество
$query ="SELECT count(*) FROM table WHERE code LIKE ? OR code_description LIKE ?";
$stmt = $db->prepare($query);
$stmt->execute(["%$search%","%$search%"]);
$count = $stmt->fetchColumn();
echo $count;
// Если нужны сами данные
$query ="SELECT * FROM table WHERE code LIKE ? OR code_description LIKE ?";
$stmt = $db->prepare($query);
$stmt->execute(["%$search%","%$search%"]);
$result = $stmt->fetchAll(PDO::FETCH_ASSOC);
var_dump($result);