mysql pregmatch_mysql查詢基於preg_match或preg_replace中的字符串?

In a previous question, I managed to get some text replacement working for my wiki. Which works great with titles. But now I'd like to replace titles with IDs. For my wiki, I simply replace [[wiki::Title]] with a link to that title. How can I get links from IDs in that title spot?

在上一個問題中,我設法為我的wiki進行了一些文本替換。哪個適用於標題。但現在我想用ID替換標題。對於我的wiki,我只需將[[wiki :: Title]]替換為該標題的鏈接。如何從該標題點的ID中獲取鏈接?

The below code works, but only outputs the ID. Example text includes [[tdh::1]] and [[tdh::5]]. I would now like to take that ID and query my database for it and use that result to build a link.

以下代碼有效,但僅輸出ID。示例文本包括[[tdh :: 1]]和[[tdh :: 5]]。我現在想要獲取該ID並查詢我的數據庫並使用該結果來構建鏈接。

$text = preg_replace("/\[\[tdh::(.+?)\]\]/","\\1", $text);

Pointing me in the right direction would be helpful. Thanks.

指出我正確的方向將是有幫助的。謝謝。

2 个解决方案

#1

2

You can use preg_replace_callback and for the callback parameter use a function that uses the ID to fetch the name from the database.

您可以使用preg_replace_callback,並且對於callback參數,使用一個使用ID從數據庫中獲取名稱的函數。

Note that above approach, while simple, could potentially result in a lot of database calls. An improvement is to first find all the IDs using preg_match_all and store them in an array. Then you can fetch all the names in a single database call. The SQL you need will be something like this:

請注意,上述方法雖然簡單,但可能會導致大量數據庫調用。一個改進是首先使用preg_match_all找到所有ID並將它們存儲在一個數組中。然后,您可以在單個數據庫調用中獲取所有名稱。你需要的SQL將是這樣的:

SELECT id, name

FROM links

WHERE id IN (?, ?, ?)

Watch out for SQL injection vulnerabilities if you aren't using parameterized queries. Store the results in an associative array with the ID as the key and the name as the value.

如果您不使用參數化查詢,請注意SQL注入漏洞。將結果存儲在關聯數組中,其中ID作為鍵,名稱作為值。

Finally you can use preg_replace_callback where the callback function finds the name from the array.

最后,您可以使用preg_replace_callback,其中回調函數從數組中找到名稱。

#2

1

You should first read the id and any other association necessary from the database and cache them in some sort of array.

您應該首先從數據庫中讀取id和任何其他必要的關聯,並將它們緩存在某種數組中。

The you use a preg_replace_callback to to the actual replacement based on that cached data.

您使用preg_replace_callback根據緩存的數據進行實際替換。

Alternatively you could to a two step process: try once and record the id's found (not replacement). Use those to query the db for additional data. Do another search and replace the results.

或者,您可以執行兩個步驟:嘗試一次並記錄找到的ID(不替換)。使用這些來查詢數據庫以獲取其他數據。再做一次搜索並替換結果。

You could also do what @Mark suggested but if the page you are editing has 15 title you will make 15 queries to the db and that might work or not for you. In some high load it will probably not work. This means slow pages usually.

您也可以執行@Mark建議的操作,但如果您正在編輯的頁面有15個標題,您將對數據庫進行15次查詢,這可能對您有效。在某些高負荷下,它可能無法正常工作。這通常意味着慢速頁面。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值