Надо чтобы функция возвращала строку?
$str = '2 3 4 5 6';
$res = preg_replace_callback( '#(\d+)#', function($match) {
return pow($match[0], 2);
}, $str
);
echo $res; // 4 9 16 25 36
<?php
$array = [10, 20, 30, 40, 50];
$arrayIterator = new ArrayIterator($array);
foreach ($arrayIterator as $key => $value) {
// Преобразование числового ключа в строку и добавление префикса
$newKey = 'key_' . $key;
// Удаление старого ключа
$arrayIterator->offsetUnset($key);
// Установка нового ключа с тем же значением
$arrayIterator->offsetSet($newKey, $value);
}
// Преобразование итератора обратно в массив
$newArray = iterator_to_array($arrayIterator);
// Вывод нового массива
print_r($newArray);
?>
<?php
function getNestedValue($array, $path) {
foreach ($path as $key) {
if (!isset($array[$key])) {
return null;
}
$array = $array[$key];
}
return $array;
}
$ar = array(
"z" => array(
"x" => array(
"c" => array(
"v" => array(
"a" => 123,
"b" => 321
)
)
)
)
);
$path = array("z", "x", "c");
$result = getNestedValue($ar, $path);
echo $result !== null ? "Значение: " . print_r($result, true) : "Элемент не найден";
?>
SELECT
ch.id_element,
ch.`table`,
ch.param,
GROUP_CONCAT(cu.name) AS value_new
FROM
c_history ch
LEFT JOIN
c_object co ON co.id = ch.id_element
LEFT JOIN
c_user cu ON FIND_IN_SET(cu.id, ch.value_old)
WHERE
ch.`table` = 'c_object'
AND ch.param = 'ids_user_merge'
GROUP BY
ch.id_element, ch.`table`, ch.param;
$basketItems = $test->getCollection()->toArray();
foreach ($basketItems as $item) {
$productId = $item->getField('PRODUCT_ID');
echo $productId;
}
? <?php
$folder = "Название папки на кириллице";
$encodedFolder = mb_convert_encoding($folder, "UTF7-IMAP", "UTF-8");
$imapStream = imap_open("{imap.example.com}" . $encodedFolder, $login, $password);
if ($imapStream === false) {
echo "Ошибка: " . imap_last_error();
exit;
}
$encodedFolder = imap_utf7_encode($folder);
<?php
use Exception;
use Imagick;
abstract class NoBrightPixelsChecker
{
protected string $imagePath;
protected string $thresholdColor;
protected Imagick $imagick;
public function __construct(string $imagePath, string $thresholdColor = '#eeeeee')
{
$this->imagePath = $imagePath;
$this->thresholdColor = $thresholdColor;
$this->loadImage();
}
private function loadImage(): void
{
try {
$this->imagick = new Imagick($this->imagePath);
} catch (Exception $e) {
throw new Exception('Failed to load image: ' . $e->getMessage());
}
}
abstract public function checkImage(): bool;
}
class ThresholdMaskNoBrightPixelsChecker extends NoBrightPixelsChecker
{
public function checkImage(): bool
{
list($rThreshold, $gThreshold, $bThreshold) = sscanf($this->thresholdColor, "#%02x%02x%02x");
$imageClone = clone $this->imagick;
$thresholdColor = new ImagickPixel("rgb($rThreshold, $gThreshold, $bThreshold)");
$imageClone->thresholdImage($thresholdColor);
$histogram = $imageClone->getImageHistogram();
$whitePixelCount = 0;
foreach ($histogram as $pixel) {
$color = $pixel->getColor();
if ($color['r'] == 255 && $color['g'] == 255 && $color['b'] == 255) {
$whitePixelCount += $pixel->getColorCount();
}
}
return $whitePixelCount === 0;
}
}
// Пример использования
try {
$checker = new ThresholdMaskNoBrightPixelsChecker('path/to/your/image.jpg');
$result = $checker->checkImage();
echo $result ? 'No bright pixels found' : 'Bright pixels found';
} catch (Exception $e) {
echo 'Error: ' . $e->getMessage();
}
?>
use libphonenumber\PhoneNumberUtil;
use libphonenumber\NumberParseException;
public function validatePhone($phone, $country): bool
{
$phoneUtil = PhoneNumberUtil::getInstance();
try {
$phoneNumber = $phoneUtil->parse($phone, strtoupper($country));
return $phoneUtil->isValidNumber($phoneNumber);
} catch (NumberParseException $e) {
return false;
}
}
import { parsePhoneNumberFromString } from 'libphonenumber-js';
$("#phoneinput").on('countrychange', function () {
const country = window.iti.getSelectedCountryData().iso2.toUpperCase();
const phone = $("#phoneinput").val();
const phoneNumber = parsePhoneNumberFromString(phone, country);
if (phoneNumber && phoneNumber.isValid()) {
// Номер телефона валиден
} else {
// Номер телефона не валиден
}
});
$folder = addslashes("\\assets\\img\\product\\");
$stmt = $mysql->prepare("INSERT INTO `catalog`(`Name_Product`, `Operator`, `Internet`, `Minuts`, `Sms`, `Price_tarif`, `Price_sim`, `image`) VALUES (?, ?, ?, ?, ?, ?, ?, ?)");
$stmt->bind_param("ssssssss", $Name_Product, $Operator, $Internet, $Minuts, $Sms, $Price_tarif, $Price_sim, $photoname);
$stmt->execute();
$output = null;
$retval = null;
exec("sudo ipset add bad_ips $bad_ip", $output, $retval);
if ($retval == 0) {
echo "IP успешно добавлен";
} else {
echo "Ошибка при добавлении IP";
}