如何为页面中的 运行 脚本创建简码函数?

How do you create a shortcode function to run a script within a page?

我将 ckeditor 用于自定义 cms 解决方案。它运行良好,但我想对其进行设置,以便管理员可以在文本区域中输入一些格式化的文本代码,例如

{print gallery[1]}
{run gallery.php[1]}
{do gallery.php[1,2,3]}
{gallery.php?id=1&opt=3}  // preferred

它会转换代码以使用提供的选项打印图库脚本的输出,gallery.php。在显示页面上,如果我执行以下操作:

$text = str_replace ("{","<?php",$text);
$text = str_replace ("}","?>",$text);

这会破坏页面。使用 str_replace 可以很好地进行简单的替换,也许是格式化,但这里不是这样。

添加注释:2018-05-17

我按照建议使用了 preg_match,这让我更接近可用的解决方案。我将以下文本添加到名为 introtext 的 textarea 字段中,管理员可能会这样做:

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus mollis convallis justo, vitae pretium libero condimentum et. {simple-gallery.php 1 3} Morbi nibh nibh, blandit sit amet sodales ullamcorper, ultricies vel nulla. {simple-gallery.php 2 3} Quisque cursus erat eu felis vulputate, scelerisque tempor libero egestas. Inte ger vitae dignissim odio, eget congue mauris. Suspendisse mattis efficitur sem, ut sagittis magna sodales eu.

在我的脚本中我使用了preg_match然后拆分结果得到:

text_to_be_replaced: simple-gallery.php 1 3
Match found and text extrapolated:
script: simple-gallery.php
arg1: 1
arg2: 3

我能够成功包含简单的-gallery.php 文件并使用 include 显示第一个幻灯片。此时,当我打印出文本区域时,它正在打印括号内的文本。我可以通过执行 str_replace 并删除文本来解决这个问题,至少在第一次出现时是这样。

然而,我们需要多功能性来在文本区域中添加多个图库,以及在上方、下方和中间的自由文本,如 introtext 中所示。我的代码:

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-05-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

观看演示:http://www.dottedi.biz/demo/code/public/shortcode/shortcode.php

使用正则表达式后:preg_match_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} 的实例替换为您的实际画廊代码。然后继续进行下一场比赛并再次执行相同的操作,您只需要将参数传递给画廊代码即可。

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;
---------------