dtrhd2850 2016-02-19 14:46 采纳率: 0%
浏览 24
已采纳

使用ID项构建链接

I'm developing an app (PHP + CodeIgniter) that has some messages (modal messages) like:

"Invoice #212 generated" or "This invoice is associated with payment #685."

Well, that ID numbers I wanna make a link to redirect user to the respective item (invoice, payment, product, person, whatever...)

So, I thought to develop at server-side, while building messages like:

str="Invoice#212 generated.";
replace(str, "invoice#222", "<a href='invoice/222'>invoice #222</a>")

Of course, using regex or something like that.

Is this the best way? Any suggestion?


Messages are generated inside actions, using global function "set_message" And Messages as "printed" inside header, using function "echo_message"

  • 写回答

1条回答 默认 最新

  • doucai1901 2016-02-19 15:04
    关注

    In PHP you could have something like this (rough generic example):

    $string = 'Invoice #212 generated'; // or 'See payment #121'
    $string = preg_replace('/(\w+) #(\d+)/', '<a href="/$1/$2">$0</a>', $string);
    

    Pretty flexible if you don't want to change previously generated strings.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?