这是替换标签的最佳/正确方法吗?
在我的文本中,用户可以输入如下所示的模块: [[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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是的,这是一个好方法。
Yes, it's a good way to do it.
您可以使用正则表达式 - 根据您的需要:
将给出所有模块/标识符的数组:
第一个子项包含摘录作为带括号的数组子项(如果您需要这些)第二个子项包含内容数组....
这将适用于您希望的尽可能多的参考文献。
You could use a regular expression- depending on your needs:
Would give an array of all the modules/identifiers:
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.