这是替换标签的最佳/正确方法吗?

发布于 2024-10-01 04:00:31 字数 1071 浏览 9 评论 0原文

在我的文本中,用户可以输入如下所示的模块: [[form:contact]],代表模块及其名称; [[module:name]

我使用以下代码从文本中撤回此内容,并将其拆分,从数据库中获取我想要的信息并替换它。

这是最好的方法吗?它工作得很好,但我不确定这是否是最有效的方法,并且想知道你们会怎么想......

我正在使用 Zend Framework。

这是代码:

$start = strpos($this->view->page->text, "[[");
$end = strpos($this->view->page->text, "]]");
$length = $end-$start+2;

$getModuleTag = substr($this->view->page->text, $start, $length);
$length = $end-$start-2;
$removeTags = substr($getModuleTag, 2, $length);

$split = strpos($removeTags, ":");

$GetModuleSort = substr($removeTags, 0, $split);
$GetModuleName = substr($removeTags, $split+1);

// Get the wanted data from the database
switch($GetModuleSort)
{
    case 'form':
        $this->result = $this->formsService->GetFormByName($GetModuleName);
        $replaceTag = $this->result->elements;
    break;
}

// TEST OUTPUT
$final = str_replace($getModuleTag, $replaceTag, $this->view->page->text);
$this->view->finalOutput = $final;

In my text the user can input modules which look like this; [[form:contact]], representing a module and its name; [[module:name]

I use the following code to retract this from the text, and split it, get the information from the database I want and replace it.

Is this the best way? It works good, but I'm not sure if this is the most efficient way, and was wondering whay you guys would think of it...

I'm working with the Zend Framework.

This is the code:

$start = strpos($this->view->page->text, "[[");
$end = strpos($this->view->page->text, "]]");
$length = $end-$start+2;

$getModuleTag = substr($this->view->page->text, $start, $length);
$length = $end-$start-2;
$removeTags = substr($getModuleTag, 2, $length);

$split = strpos($removeTags, ":");

$GetModuleSort = substr($removeTags, 0, $split);
$GetModuleName = substr($removeTags, $split+1);

// Get the wanted data from the database
switch($GetModuleSort)
{
    case 'form':
        $this->result = $this->formsService->GetFormByName($GetModuleName);
        $replaceTag = $this->result->elements;
    break;
}

// TEST OUTPUT
$final = str_replace($getModuleTag, $replaceTag, $this->view->page->text);
$this->view->finalOutput = $final;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

轻许诺言 2024-10-08 04:00:39

是的,这是一个好方法。

Yes, it's a good way to do it.

深白境迁sunset 2024-10-08 04:00:37

您可以使用正则表达式 - 根据您的需要:

$pattern="/\[\[(.*?)\]\]/";
$string="some string with a [[module]] in it, and [[another]] here";
$match=preg_match_all($pattern, $string, $matches);
print_r($matches);

将给出所有模块/标识符的数组:

Array ( [0] => Array ( [0] => [[module]] [1] => [[another]] ) [1] => Array ( [0] => module [1] => another ) ) 

第一个子项包含摘录作为带括号的数组子项(如果您需要这些)第二个子项包含内容数组....

这将适用于您希望的尽可能多的参考文献。

You could use a regular expression- depending on your needs:

$pattern="/\[\[(.*?)\]\]/";
$string="some string with a [[module]] in it, and [[another]] here";
$match=preg_match_all($pattern, $string, $matches);
print_r($matches);

Would give an array of all the modules/identifiers:

Array ( [0] => Array ( [0] => [[module]] [1] => [[another]] ) [1] => Array ( [0] => module [1] => another ) ) 

The first child includes the extracts as array children with the brackets (if you need these) the second child contains an array of the contents....

This would work with as many references as you wish.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文