Php 如何创建一个短代码函数来运行页面中的脚本?
我正在使用ckeditor定制cms解决方案。它工作得很好,但我想设置它,以便管理员可以在文本区域输入一些格式化的文本代码,如Php 如何创建一个短代码函数来运行页面中的脚本?,php,ckeditor,Php,Ckeditor,我正在使用ckeditor定制cms解决方案。它工作得很好,但我想设置它,以便管理员可以在文本区域输入一些格式化的文本代码,如 {print gallery[1]} {run gallery.php[1]} {do gallery.php[1,2,3]} {gallery.php?id=1&opt=3} // preferred 它将转换代码以打印gallery脚本gallery.php的输出,并提供选项。在显示页面上,如果执行以下操作: $text = str_replace ("
{print gallery[1]}
{run gallery.php[1]}
{do gallery.php[1,2,3]}
{gallery.php?id=1&opt=3} // preferred
它将转换代码以打印gallery脚本gallery.php的输出,并提供选项。在显示页面上,如果执行以下操作:
$text = str_replace ("{","<?php",$text);
$text = str_replace ("}","?>",$text);
在我的脚本中,我使用了preg_match,然后分割结果并得到:
text_to_be_replaced: simple-gallery.php 1 3
Match found and text extrapolated:
script: simple-gallery.php
arg1: 1
arg2: 3
我能够成功地包含simple-gallery.php文件,并使用include显示第一个幻灯片。此时,当我打印文本区域时,它正在打印括号内的文本。我可以通过stru替换和剥离文本来解决这个问题,至少在第一次出现时是这样
然而,我们需要多功能性来添加文本区域中的多个图库,以及上面、下面和中间的自由文本,如介绍文本所示。我的代码:
preg_match_all("/\{(.+?)\}/", $introtext,$results);
$array1 = $results[1];
$text_to_be_replaced1 = "$array1[0]";
$split1 = explode(' ',$text_to_be_replaced1);
$script1 = $split1[0];
$arg1 = $split1[1]; // if needed
$arg2 = $split1[2]; // if needed
echo $introtext; // returns everything including the bracketed text
include ("$script1");
2018年5月20日新增注释
我修改了守则;它正在添加一个参数化的多媒体资料或视频脚本,但除了在多媒体资料代码所属的文本区域中输出1(数字1)外,还有些顺序混乱。修订守则:
preg_match_all("/\{(.+?)\}/", $introtext,$results);
foreach($results[1] as $gallery){
$split = explode(' ',$gallery);
$arg1 = isset($split[0]) ? $split[0] : -1;
$arg2 = isset($split[1]) ? $split[1] : -1;
$arg3 = isset($split[2]) ? $split[2] : -1;
$html = include ("$arg1");
$introtext = preg_replace("/\{" . $gallery . "\}/", $html, $introtext);
}
echo $introtext;
它显示如下:
gallery #1
gallery #2
gallery/video #3
text
1
text
1
text
1
使用正则表达式后,请参见演示::
preg\u match\u all(“/\{(+?)\}/”,$introtext,$results)代码>
您可以这样做:
foreach($results[1] as $gallery){
$split = explode(' ',$gallery);
$arg1 = isset($split1[0]) ? $split1[0] : -1; // if needed && check if the parameter exists before trying to access it
$arg2 = isset($split1[1]) ? $split1[1] : -1; // if needed && check if the parameter exists before trying to access it
$html = 'your code for the gallery';
$introtext = preg_replace("/\{" . $gallery . "\}/", $html, $introtext);
}
它将用实际的库代码替换{gallery.php 1 2}的实例。然后转到下一个匹配项,再次执行同样的操作,只需将参数传递给库代码。最好的选择是使用正则表达式。例如,`preg\u match\u all(“/\{(+?)\}/”,$your\u string,$results);然后解析结果并显示Gallery我添加了一些进度说明;在设置输出以显示简介文本和在正确位置多次使用短代码方面仍然需要帮助。我添加了一些额外的进度说明。越来越近了。
want to improve above answer:
preg_match_all("/\{(.+?)\}/", $introtext, $results);
foreach ($results[1] as $gallery) {
$split = explode(' ', $gallery);
$arg1 = isset($split[0]) ? $split[0] : -1;
$arg2 = isset($split[1]) ? $split[1] : -1;
$arg3 = isset($split[2]) ? $split[2] : -1;
ob_start();
include "$arg1";
$html = ob_get_contents();
ob_end_clean();
$introtext = preg_replace("/\{" . $gallery . "\}/", $html, $introtext);
}
echo $introtext;
---------------
want to improve above answer:
preg_match_all("/\{(.+?)\}/", $introtext, $results);
foreach ($results[1] as $gallery) {
$split = explode(' ', $gallery);
$arg1 = isset($split[0]) ? $split[0] : -1;
$arg2 = isset($split[1]) ? $split[1] : -1;
$arg3 = isset($split[2]) ? $split[2] : -1;
ob_start();
include "$arg1";
$html = ob_get_contents();
ob_end_clean();
$introtext = preg_replace("/\{" . $gallery . "\}/", $html, $introtext);
}
echo $introtext;
---------------