Bob van Luijt
Bob van Luijt

Reputation: 7588

PHP regex find string with multiple values

My question concers this string: Mage::helper('catalog/image')->init($product, 'image', $image->getFile())->resize(265)

I'm trying to find a way to find this string. but... in the string are: $product, image and 265 variable. So I need to find strings like:
Mage::helper('catalog/image')->init($product, 'image', $image->getFile())->resize(265) Mage::helper('catalog/image')->init($product, 'thumb', $image->getFile())->resize(75) Mage::helper('catalog/image')->init($image, 'mini', $image->getFile())->resize(25) etc...

For now I have this, but it just searches throug files for Mage::helper('catalog

function listFolderFiles($dir){
    $ffs = scandir($dir);
    foreach($ffs as $ff){
        if($ff != '.' && $ff != '..'){
            if(is_dir($dir.'/'.$ff)) listFolderFiles($dir.'/'.$ff);
            if(strpos($ff, '.phtml')!==false){
                $contents = file_get_contents($dir.'/'.$ff);
                $pattern = preg_quote("Mage::helper('catalog", "/");
                $pattern = "/^.*$pattern.*\$/m";
                if(preg_match_all($pattern, $contents, $matches)){
                   echo "Found matches in: ".$dir.'/'.$ff."\n";
                }
                else{
                   echo "No matches found in: ".$dir.'/'.$ff."\n";
                }
            }
        }
    }
}

listFolderFiles('../app/design');

Upvotes: 0

Views: 424

Answers (1)

MikeM
MikeM

Reputation: 13631

There is no apparent need for "/^.*$pattern.*\$/m", preg_match_all or $matches

$pattern = "/Mage::helper\('catalog\/image'\)->init\([^)]+?image->getFile\(\)\)->resize\(\d+\)/";
if ( preg_match($pattern, $contents) ) {
    // do stuff
}

This will match any string containing "Mage::helper('catalog/image')->init(Ximage->getFile())->resize(Y)",
where X is one or more characters that are not ), and Y is one or more digits.

Upvotes: 1

Related Questions