我正在开发一个应用程序(PHP + CodeIgniter),其中包含一些消息(模式消息),例如:
"Invoice #212 generated" or "This invoice is associated with payment #685."
好吧,我想创建一个链接,将用户重定向到相应的项目(发票、付款、产品、人员,等等......)
所以,我想在服务器端进行开发,同时构建如下消息:
str="Invoice#212 generated.";
replace(str, "invoice#222", "<a href='invoice/222'>invoice #222</a>")
当然,使用正则表达式或类似的东西。
这是最好的方法吗?有什么建议吗?
<小时/>消息是使用全局函数“set_message”在操作内部生成的 和 使用函数“echo_message”在 header 中“打印”消息
最佳答案
在 PHP 中,你可以有这样的东西(粗略的通用示例):
$string = 'Invoice #212 generated'; // or 'See payment #121'
$string = preg_replace('/(\w+) #(\d+)/', '<a href="/$1/$2">$0</a>', $string);
如果您不想更改以前生成的字符串,则非常灵活。
关于javascript - 与 ID 项建立链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35508009/