2013-03-03 193 views
0

我的问题concers这个字符串:Mage::helper('catalog/image')->init($product, 'image', $image->getFile())->resize(265)PHP正则表达式查找字符串包含多个值

我试图找到一种方法来找到这个字符串。但是...在字符串中是:$ product,image和265变量。 所以,我需要找到像字符串:
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) 等...

现在我有这个,但它只是搜索通过量文件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'); 
+1

你可以使用“$ pattern =”而不是“$ pattern。=”将下一个模式附加到正则表达式中吗? – EGOrecords 2013-03-03 11:30:23

+0

是的,它也正常工作,像这样:-)我只需要扩展它... – 2013-03-03 11:34:41

+0

很高兴帮助。它现在工作吗? (使用正则表达式时只是一个提示:打印出来,把它放到一个真正的正则表达式框中,看看它是什么构建你的!) – EGOrecords 2013-03-03 11:36:06

回答

1

有没有明显需要"/^.*$pattern.*\$/m",preg_match_all$matches

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

这将匹配含"Mage::helper('catalog/image')->init(Ximage->getFile())->resize(Y)"
任何字符串其中X是一个或多个字符不属于),并Y是一个或多个数字。

+0

您先生,真棒,我可以使用这个!你的解释是非常有用的。 – 2013-03-03 12:38:17

相关问题