Plugins 字符串替换为数组
我正在尝试为joomla 3创建一个简单的图像库。我已经设法让代码正常工作了。它的工作方式是,它从文章中检测字符串{dGalley}stories{/dGallery}。到目前为止,我已经这样做了。它可以工作,但只打印一张图像。有什么建议吗?提前谢谢Plugins 字符串替换为数组,plugins,joomla,Plugins,Joomla,我正在尝试为joomla 3创建一个简单的图像库。我已经设法让代码正常工作了。它的工作方式是,它从文章中检测字符串{dGalley}stories{/dGallery}。到目前为止,我已经这样做了。它可以工作,但只打印一张图像。有什么建议吗?提前谢谢 <?php defined('_JEXEC') or die; class PlgContentgallery extends JPlugin { public function onContentPrepare($content
<?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++;
}
}
}
你想要更像这样的吗
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);
}
}
}
}
您是否也试图从子文件夹中获取图像 这是怎么回事$匹配[1][$i]。。。这不应该是$value吗?我不确定$value[$I]应该是什么,因为我不认为preg_match_都返回多维数组。你确定你在那里得到了一个有效的文件夹吗?还有,为什么不直接使用JFilesystem::files$matches[1]来获取文件夹中的文件数组呢?我试图做的是,站点上的页面可能多次出现字符串{dGallery}foldername1{/dGallery}、{dGallery}foldername2{/dGallery}等等$匹配项[1]的数组类似于数组'0'=>Folder1','1'=>Folder2。我在$match[1]数组中循环,$matches[1]$match[1][$I=0]中的另一个循环是Fo1,Fo2,读取这些文件夹获取图像并用图像替换{dG..}Fo1{/dG..}字符串。希望有意义。请原谅我的英语…谢谢埃琳的帮助。如果文件夹中只有一个图像,则解决方案有效。每个文件夹中都有许多图像。我已经发布了更新的代码,但有点混乱。谢谢。我不知道为什么会这样,关键是要使用API来获取所有导入的数组-你不应该重新发明这个轮子。同样如前所述,您应该检查$dir是否存在,而不是硬编码路径。