如何在 PHP 中创建 WordPress 短代码样式函数
我正在尝试在 PHP 中创建 Wordpress 短代码样式功能,以用图像替换“[[133]]”等短代码。基本上,我有一个 ID 为 1-150 的图像 URL/标题/字幕的 MySQL 表,我希望能够使用如下短代码将它们动态插入到我的页面文本中:
Blabla bla bla bla bla。 [[5]] 另外,啦啦啦啦啦啦啦[[27]] 嘿,还有布拉布拉布拉! [[129]]
所以,我只想获取 ID 作为 $id,然后将其提供给 MySQL 查询,例如 mysql_query("从图像中选择标题、副标题、url,其中 id = $id") 然后将“[[id]]”替换为图片/标题/副标题。我希望能够在同一页面上多次执行此操作。
我知道这必须涉及正则表达式和 preg_match、preg_replace、strstr、strpos、substr ... 的某种组合,但我不知道从哪里开始以及应该使用哪些函数来做哪些事情。你能推荐一个策略吗?我不需要代码本身——只要知道哪些部分使用什么就会非常有帮助。
I am trying to create a Wordpress shortcode-style feature in PHP to replace shortcodes like "[[133]]" with images. Basically, I have a MySQL table of image URLs/titles/subtitles with IDs 1-150, and I want to be able to dynamically insert them into the text of my pages with shortcodes like this:
Blabla bla bla bla bla. [[5]] Also, bla bla bla bla bla [[27]]
Hey, and bla bla bla! [[129]]
So, I just want to grab the ID as $id, and then feed it to a MySQL query like
mysql_query("SELECT title,subtitle,url FROM images WHERE id = $id")
and then replace the "[[id]]" with the img/title/subtitle. I would like to be able to do this multiple times on the same page.
I know this has to involve regex and some combination of preg_match, preg_replace, strstr, strpos, substr... but I don't know where to start and which functions I should be using to do which things. Can you recommend a strategy? I don't need the code itself—just knowing what to use for which parts would be extremely helpful.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
如果您希望能够编写这样的短代码:
这里有一个更完整的方法,使用
preg_replace_callback
和call_user_func_array
来实现参数化短代码。如果定义了这个函数:
那么这个调用
将输出:
这只是我现在基于 supertrue 的想法实现的东西。
任何反馈都非常受欢迎。
If you want to be able to write shortcodes like this :
here is a more complete way, using
preg_replace_callback
andcall_user_func_array
to implement parameterized shortcodes.If this function is defined :
Then this call
Will output :
This is just something I implemented right now based on supertrue's idea.
Any feedback is more than welcome.
使用执行 MySQL 查询并格式化替换文本的函数
getimage($id)
,这几乎可以完成您需要的一切:我只需要弄清楚要放入其中的内容
getimage()
(其中 ?????? 是),这将使其放入正确的[[id]]
的正确图像中。参考
preg_match_all
< /a> 和preg_replace
有关更多详细信息,请参阅官方文档。With a function
getimage($id)
that does the MySQL query and formats the replacement text, this almost does everything you need:I just need to figure out what to put inside
getimage()
(where ????? is) that will make it put in the right image for the right[[id]]
.Refer
preg_match_all
andpreg_replace
on official documentation for more details.为此,可以采取各种不同的方法,具体取决于您打算如何显示等,
以句子“Hello [34] world”为例,
创建一个简单的函数,例如replaceCode($string)
如果发现更多出现的括号,则递归调用该函数再次将字符串的其余部分再次传递给函数。
注意:可能需要首先进行验证以确保 [ 和 ] 正确平衡!
Various different approaches can be taken for this, depending on how you plan to display ect,
Take the sentence "Hello [34] world"
Create a simple function e.g replaceCode($string)
If anymore occurrences of brackets are found, recursively call the function again, passing the rest of the string to the function again.
Note: Validation may need to be done first to ensure the [ and ] are properly balanced!
我的赌注是 PHP 的 strtr 函数...
它的输出是
它在 5.6 上运行良好。
My bet is PHP's strtr function...
it's output is
It is working fine on 5.6.
我相信正则表达式是:(
双括号内的 1 到 3 位数字。)
The regex, I believe, would be:
(for a 1-to-3 digit number inside double brackets.)