File: /mnt/data/smarthr-co-in/demo/cakephp/template/convert-imgs.php
<?php
$inputFile = 'templates/element/modal-popup-old.php';
$outputFile = 'templates/element/modal-popup.php';
$content = file_get_contents($inputFile);
// 1. Convert <img ...> tags
$patternImg = '/<img\s+([^>]*?)src="([^"]+)"([^>]*)>/i';
$content = preg_replace_callback($patternImg, function($matches) {
$before = trim($matches[1]);
$src = $matches[2];
$after = trim($matches[3]);
$attrs = trim($before . ' ' . $after);
preg_match_all('/(\w+)="([^"]*)"/', $attrs, $attrMatches, PREG_SET_ORDER);
$attrArray = [];
foreach ($attrMatches as $attr) {
$key = $attr[1];
$value = $attr[2];
if (strtolower($key) === 'src') continue;
$attrArray[] = "'$key' => '$value'";
}
$attrString = implode(', ', $attrArray);
// Remove leading ./ or assets/img/ for CakePHP convention
$src = preg_replace('#^(\.?/?assets/img/)#', '', $src);
return "<?= \$this->Html->image('$src', [$attrString]) ?>";
}, $content);
// 2. Convert data-image="..." attributes in any tag
$patternDataImage = '/data-image="([^"]+)"/i';
$content = preg_replace_callback($patternDataImage, function($matches) {
$src = $matches[1];
// If already contains PHP, skip replacement
if (strpos($src, '<?') !== false) {
return $matches[0];
}
// Remove leading ./ or assets/img/ for CakePHP convention
$src = preg_replace('#^(\.?/?assets/img/)#', '', $src);
// You can add more attributes if needed
return 'data-image="<?= $this->Html->image(\'' . $src . '\') ?>"';
}, $content);
file_put_contents($outputFile, $content);
echo "Conversion complete! Check $outputFile\n";