字符串替换为数组

时间:2014-03-08 21:02:10

标签: plugins joomla

我正在尝试为joomla 3创建一个简单的图片库。我已经设法让代码正常工作。它的工作方式是,它从文章中检测字符串{dGalley}stories{/dGallery}。到目前为止我已经这样做了。它工作,但它只打印一个图像。有什么建议吗?提前谢谢。

<?php

defined('_JEXEC') or die;

class PlgContentgallery extends JPlugin
{
    public function onContentPrepare($content, $article, $params, $limit){

        preg_match_all('/{dGallery}(.*?){\/dGallery}/is', $article->text, $matches);

        $i=0;

        foreach ($matches[1] as $value)
        {

            $result = array(); 

            $dir = '/home/juniorwe/public_html/images/'.$matches[1][$i];

            $cdir = scandir($dir); 

            foreach ($cdir as $key => $value) 
            { 
                if (!in_array($value,array(".",".."))) 
                { 
                    if (is_dir($dir . DIRECTORY_SEPARATOR . $value)) 
                    { 
                        $result[$value] = dirToArray($dir . DIRECTORY_SEPARATOR . $value); 
                    } 
                    else 
                    { 
                        $article->text = str_replace('{dGallery}'.$matches[1][$i].'{/dGallery}','<img src="../images/'.$matches[1][$i].'/'.$value.'"/>
', $article->text);

                    } 
                }

            } 
            $i++;
        }

    }

}

更新了代码。它有效,但有点脏解决方案:

<?php
defined('_JEXEC') or die;
class PlgContentgallery extends JPlugin
{
    public function onContentPrepare($content, $article, $params, $limit)
    {

        $document = JFactory::getDocument();

        $document->addScript(JURI::base(). "plugins/content/gallery/jquery.colorbox.js");
        $document->addScript(JURI::base(). "plugins/content/gallery/colorboxCall.js");
        $document->addStyleSheet(JURI::base(). "plugins/content/gallery/colorbox.css");

        preg_match_all('/{dGallery}(.*?){\/dGallery}/is', $article->text, $matches);

        $i=0;

        foreach ($matches[1] as $value)
        {
            $result .= ''; 
            $dir = '/home/juniorwe/public_html/images/'.$matches[1][$i];
            $cdir = scandir($dir); 
            foreach ($cdir as $key => $value) 
            { 
                if (!in_array($value,array(".",".."))) 
                { 
                    if (is_dir($dir . DIRECTORY_SEPARATOR . $value)) 
                    { 
                        $result[$value] = dirToArray($dir . DIRECTORY_SEPARATOR . $value); 
                    }else{ 
                        $result .='<a class="group4" href="../images/'.$matches[1][$i].'/'.$value.'" title=""><img src="../images/'.$matches[1][$i].'/'.$value.'" class="gallery"/></a>';
                    } 
                }
            }
            $article->text = str_replace('{dGallery}'.$matches[1][$i].'{/dGallery}',$result, $article->text); 
            $i++;
        }
    }
}

1 个答案:

答案 0 :(得分:0)

你想要更像这样的东西

public function onContentPrepare($content, $article, $params, $limit)
{

        preg_match_all('/{dGallery}(.*?){\/dGallery}/is', $article->text, $matches);


        foreach ($matches[0] as $folder)
        {

            $result = array(); 

            $dir = '/home/juniorwe/public_html/images/' . $folder; // You might want to get the stored value for image path instead of hard coding

            if (is_dir($dir))
            { 
                 $cdir = JFilesystem::files($dir); 

                 foreach ($cdir as $value) 
                { 
                        $article->text = str_replace('{dGallery}' . $folder .'{/dGallery}','<img src="/images/' . $folder . '/' . $value. ' "/>', $article->text);
                }

            } 

        }
 }

您是否也尝试从子文件夹中获取图像?