PHP:带 SQL 选择的 BBCode?
我想用 SQL 选择编写 bbcode。基本上,我希望用户能够在文本字段中输入 [user]Anotheruser[/user]
,然后在前端将其转换为如下所示的 URL:http://mydomain.com/user/[userid]/anotheruser
。因此,为了获取用户 ID,我需要包含一个 SQL 选择,另外还需要一个 if else 来找出其他用户是否实际存在。我可以使用通常用于创建 bbcode 的 preg_replace
来完成此操作,还是需要做一些更复杂的事情?
I would like to code a bbcode with SQL selection. Basically I want the user to be able to input [user]Anotheruser[/user]
in a text field, which then is converted in the front-end to an URL that looks like this: http://mydomain.com/user/[userid]/anotheruser
. So in order to get the userid, I'd need to include an SQL selection, and additionally an if else to find out of the other user actually exists. Can I do this with a preg_replace
as usually used for creating bbcode, or do I need to do something more complex?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
pre_replace 应该可以工作,但要小心。确保修补所有漏洞...尤其要注意 sql 注入。
pre_replace should work, be careful tho. Make sure you patch any holes...watch out for sql injection in particular.
您应该避免在 POST 端注入,就像您在其他地方应该做的那样。
这种情况没有什么不同。
You should avoid injections POST-side, just like you should do everywhere else.
This situation is no different at all.
首先使用
preg_match
标签,获取用户名,执行查询(小心并确保该步骤安全!)并用新的 url 替换整个匹配的字符串:preg_match
the tag first, get the user name, execute the query (be careful and make that step safe!) and replace the whole matched string with the new url: