0

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');
4
  • 1
    are you shure to use " $pattern =" and not " $pattern .=" to append the next patterns to the regex? Commented Mar 3, 2013 at 11:30
  • yes, it also works fine like this :-) I just need to extend it... Commented Mar 3, 2013 at 11:34
  • Glad to help. Is it working now? (Just a tip when using RegEx: Print it out, put it into a live-regex-box and see what it builds you!) Commented Mar 3, 2013 at 11:36
  • Hi EGORecords, It was already working, I need some help to find Mage::helper('catalog/image')->init($product, 'image', $image->getFile())->resize(265) and not only Mage::helper('catalog Commented Mar 3, 2013 at 11:42

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

You sir, are awesome, I can use this! your explanation was really useful.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.