Tinymce,编辑器中的PHP变量

发布于 2025-02-09 20:45:05 字数 861 浏览 0 评论 0原文

我知道我们不应该在Tinymce内容编辑器中编写PHP代码。但这是我的困境:我正在构建的网站上的URL包括用户语言代码,例如test.com/en/products/product-1。当通过Tinymce编辑器添加/编辑内容时,有时需要在文本中添加超链接,例如“单击此处有关更多详细信息”。如果内容编辑器写入< a href =“/products/products/product1”> tere</a>“,此URL将无效。链接需要为< a href =“/<?php echo $ lang; ?>/products/product1“> tere</a>。但是Tinymce要么删除此代码或使其无法使用。

我尝试将其添加到配置中:

   protect: [
        /\<\/?(if|endif)\>/g, // Protect <if> & </endif>
        /<\?php.*?\?>/g // Protect php code
   ]

我如何在编辑器中启用PHP输入,或者如果有其他方法可以实现相同的方法...我还尝试添加&lt; base href =“ domain.com/<?php echo $ lang; ?&gt;“ /&gt; < /code>标签,但这无效。

它导致了:

href="/<!--mce:protected %3C%3Fphp%20echo%20%24lang%3B%20%3F%3E-->/products/product1"

I know that we're not supposed to write PHP code in the TinyMCE content editor. But here's my predicament: the URLs on the website I'm building include a user language code, e.g. test.com/en/products/product-1. When content is added/edited via the TinyMCE editor, sometimes, hyperlinks need to be added in the text, e.g. "Click here for more details". If the content editor writes <a href="/products/product1">here</a>", this URL will not be valid. The link needs to be <a href="/<?php echo $lang; ?>/products/product1">here</a>. But TinyMCE either deletes this code or makes it unusable.

I've tried adding this to the configuration:

   protect: [
        /\<\/?(if|endif)\>/g, // Protect <if> & </endif>
        /<\?php.*?\?>/g // Protect php code
   ]

How can I enable PHP input in the editor? Or if there's a different way to achieve the same... I also tried adding a <base href="domain.com/<?php echo $lang; ?>" /> tag but that doesn't work.

It results in this:

href="/<!--mce:protected %3C%3Fphp%20echo%20%24lang%3B%20%3F%3E-->/products/product1"

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

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

发布评论

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

评论(1

物价感观 2025-02-16 20:45:05

最简单,最安全的方法是在将页面发送给用户之前替换文本,而不是尝试评估PHP代码。像这样的文本中使用占位符:

<a href="/__LANG__/products/product1">here</a>

然后在将文本发送到浏览器之前,用所需的变量代替占位符

$text = strtr($text, ['__LANG__' => $lang]);

The easiest and safest way to do this would be to replace the text before the page is sent to the user, rather than attempting to evaluate PHP code. Use placeholders in your text, like this:

<a href="/__LANG__/products/product1">here</a>

Then before the text is sent to the browser, replace the placeholders with the variables you want

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