HEX
Server: nginx/1.24.0
System: Linux DGT-WORDPRESS-VM-SERVER 6.14.0-1017-azure #17~24.04.1-Ubuntu SMP Mon Dec 1 20:10:50 UTC 2025 x86_64
User: ubuntu (1000)
PHP: 8.4.12
Disabled: NONE
Upload Files
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";